首页 > 代码库 > 5.User Interface/ActionBar

5.User Interface/ActionBar

1. ActionBar

  First added in Android 3.0(API level 11)

  

 

2. Working the Action Bar

  2.1 Removing the action bar

ActionBar actionBar = getSupportActionBar();actionBar.hide();  

  2.2 Using a logo instead of an icon

    By default, the system uses your application icon in the action bar,as specified by the icon attribute in the <application>or<activity>

      element. However, if you also specify the logo attribute, then the action bar uses the logo image instead of the icon.

 

3. Adding Action Items

  

  Action bar with three action buttons and the overflow buton

  if the menu items have android:icon and android:title, it be defaluted to show only its icon. android:showAsAction add "withText" property

    to show title and icon together

 

4.Using spilt action Bar

  Split action bar provides a separate bar at the bottom of the screen to display all action items when the activity is running on a narrow

    screen (such as a portrait-oriented handset).

  

  Mock-ups showing an action bar with tabs(left), then with splite action bar(middle), and with the app icon and title disabled(right)

  To enable split action bar when using the support library, you must do two things:

    <1> Add uiOptions="splitActionBarWhenNarrow" to each <activity> or <application> element

    <2> To support older versions, add a <meta-data> element as a child of each <activity> element that declares the same value

 

      for "android.support.UI_OPTIONS".

 

5. Navigating Up with the App Icon

  Enabling the app icon as an Up button allows the user to navigate your app based on the hierarchical relationships between screens.

    For instance, if screen A displays a list of items, and selecting an item leads to screen B, then screen B should include the Up button,

    which returns to screen A.

  To enable the app icon as an Up button, call setDisplayHomeAsUpEnabled(). For example

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_details);    ActionBar actionBar = getSupportActionBar();    actionBar.setDisplayHomeAsUpEnabled(true);    ...}

  Now the icon in the action bar appears with the Up caret as shown below

  

  To specify the activity to open when the user presses Up button, you have two options:

    <1>Specify the parent activity in the manifest file.

      This is the best option when the parent activity is always the same.

      By declaring in the manifest which activity is the parent, the action bar automatically performs the correct action when the user

        presses the Up button.

<application ... >    ...    <!-- The main/home activity (has no parent activity) -->    <activity        android:name="com.example.myfirstapp.MainActivity" ...>        ...    </activity>    <!-- A child of the main activity -->    <activity        android:name="com.example.myfirstapp.DisplayMessageActivity"        android:label="@string/title_activity_display_message"        android:parentActivityName="com.example.myfirstapp.MainActivity" >        <!-- Parent activity meta-data to support API level 7+ -->        <meta-data            android:name="android.support.PARENT_ACTIVITY"            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>

    <2>override getSupportParentActivityIntent() and onCreateSupportNavigateUpTaskStack() in the activity

  

  

6. Adding an Action View

  An action view is a widget that appears in the action bar as a substitute for an action button.

  An action view provides fast access to rich actions without changing activities or fragments, and without replacing the action bar.

  For example, if you have an action for Search, you can add an action view to embeds a SearchView widget in the action bar

android:showAsAction="ifRoom|collapseActionView"android:actionViewClass="android.widget.SearchView" //

      

  If you need to configure the action view (such as to add event listeners), you can do so during the onCreateOptionsMenu()

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.main_activity_actions, menu);    MenuItem searchItem = menu.findItem(R.id.action_search);    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);    // Configure the search info and add any event listeners    ...    return super.onCreateOptionsMenu(menu);}

  6.1 Handling collapsible action views

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    getMenuInflater().inflate(R.menu.options, menu);    MenuItem menuItem = menu.findItem(R.id.actionItem);    ...    // When using the support library, the setOnActionExpandListener() method is    // static and accepts the MenuItem object as an argument    MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {        @Override        public boolean onMenuItemActionCollapse(MenuItem item) {            // Do something when collapsed            return true;  // Return true to collapse action view        }        @Override        public boolean onMenuItemActionExpand(MenuItem item) {            // Do something when expanded            return true;  // Return true to expand action view        }    });}

 

7. Adding an Action Provider

  ....

 

8. Adding Navigation Tabs

  

  

 

  http://blog.csdn.net/guolin_blog/article/details/25466665

5.User Interface/ActionBar