1. Guidelines, examples, personal feelings
First Officer Net Guide link http://developer.android.com/guide/topics/ui/actionbar.html
Refer to the official web example http://developer.android.com/samples/ActionBarCompat-Basic/index.html
At first glance, the guide senses that Actionbar's role is similar to the include heder.html commonly used in web development, such as the total first row of cnblog pages.
2. Compatibility issues: SDK 11 and 11 above can be used directly, SDK 11 below requires SUPPORT-V7
PS: Use these two in the configuration file also to distinguish the following, note the following # is the use of SDK 11 comes with the Actionbar configuration
<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:support= "Http://schemas.android.com/apk/res-auto"# Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Tools:context= "Com.example.actionbar.MainActivity" > <ItemAndroid:id= "@+id/action_settings"android:orderincategory= "+"Android:title= "@string/action_settings"support:showasaction= "Never"#app: Showasaction= "Never"/> <ItemAndroid:id= "@+id/menu_refresh"Android:icon= "@drawable/ic_action_refresh"Android:title= "@string/menu_refresh"support:showasaction= "Ifroom" />
3. Construction method
There are configuration files, and program code to build two
@Override publicboolean oncreateoptionsmenu (Menu menu) {
configuration file Build getmenuinflater (). Inflate (R.menu.main, menu); = Menu.add (0,r.id.menu_location,0, r.string.menu_location);
Program Construction Locationitem.seticon (r.drawable.ic_action_location); Menuitemcompat.setshowasaction (Locationitem, menuitem.show_as_action_if_room); return true ; }
4. Full code
PackageCom.example.actionbar;ImportAndroid.annotation.SuppressLint;ImportAndroid.annotation.TargetApi;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.support.v4.view.MenuItemCompat;ImportAndroid.support.v7.app.ActionBar;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;Importandroid.view.menuitem;//@SuppressLint ("Newapi") Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.sample_main); Android.app.ActionBar ActionBar=Getactionbar (); Actionbar.hide (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); MenuItem Locationitem= Menu.add (0,r.id.menu_location,0, r.string.menu_location); Locationitem.seticon (r.drawable.ic_action_location); Menuitemcompat.setshowasaction (Locationitem, menuitem.show_as_action_if_room); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. Switch(Item.getitemid ()) { CaseR.id.menu_refresh://Here we might start a background refresh task return true; Caser.id.menu_location://Here we might call Locationmanager.requestlocationupdates () return true; Caser.id.menu_settings://Here we would open up our settings activity return true; Caser.id.action_settings://Here we would open up our settings activity return true; } return Super. onoptionsitemselected (item); }}
5. Follow-up .....
Android ActionBar