The action baris included in all activities that use the Theme.Holo theme (or one of its descendants),which is the default theme when either the targetSdkVersion or minSdkVersionattribute is set to "11" or higher. If you don't want the action barfor an activity, set the activity theme to Theme.Holo.NoActionBar.
You can hide theaction bar at runtime by calling hide(). For example:
ActionBar actionBar = getActionBar(); actionBar.hide();
Figure1 : Action bar with three action buttons and the overflow button.
The action barprovides users access to the most important action items relating to the app'scurrent context. Those that appear directly in the action bar with an iconand/or text are known as actionbuttons. Actions that can't fit in the action bar or aren't importantenough are hidden in the actionoverflow.
When youractivity starts, the system populates the action items by calling youractivity's onCreateOptionsMenu()method. Use this method to inflate a menu resource that defines all the actionitems.
@Override public boolean onCreateOptionsMenu(Menumenu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_activity_actions, menu); return super.onCreateOptionsMenu(menu); }To request thatan item appear directly in the action bar as an action button, include showAsAction="ifRoom"in the <item> tag. If there's not enough room for the item in the actionbar, it will appear in the action overflow.
If your menuitem supplies both a title and an icon—with the title and icon attributes—thenthe action item shows only the icon by default. If you want to display the texttitle, add "withText"to the showAsAction attribute. For example:
<?xml version="1.0"encoding="utf-8"?> <menuxmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /> </menu>Note: The "withText" value is a hint to the action bar that thetext title should appear. The action bar will show the title when possible, butmight not if an icon is available and the action bar is constrained for space.
Figure 2 : The Up button in Gmail.
Enabling the app icon as an Up button allows theuser to navigate your app based on the hierarchical relationships betweenscreens. For instance, if screen A displays a list of items, and selecting anitem leads to screen B, then screen B should include the Up button, whichreturns to screen A.
Note: Up navigation isdistinct from the back navigation provided by the system Back button. The Backbutton is used to navigate in reverse chronological order through the historyof screens the user has recently worked with. It is generally based on thetemporal relationships between screens, rather than the app's hierarchystructure (which is the basis for up navigation).
To enable the appicon as an Up button, call setDisplayHomeAsUpEnabled(true). For example:
@Override protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); ... }当用户触摸这个图标时,系统会调用Activity带有android.R.id.home ID的onOptionsItemSelected()方法。在这个响应中,你既可以启动主Activity,也可以返回你的应用程序结构化层次中用户上一步操作的界面。
例如,下例的onOptionsItemSelected()方法实现了返回应用程序的主Activity的操作:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; go home Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } }