Starting with Android3.0 (API level 11), Android devices no longer require a dedicated menu key.
With this change, Android apps should remove their reliance on traditional 6-item menus. Instead, the Anction bar is provided to provide basic user functionality.
- Create 3 Basic Menus
- Options menu and Action Bar
The Options menu is a feature that is placed in the app that has global impact, such as "search", "Send Mail", "settings", etc.
On Android3.0 and above, the Options menu is placed on the action bar.
- To create a menu of options:
- Overriding the Oncreateoptionmenu () method
1 @Override 2 Public Boolean Oncreateoptionsmenu (Menu menu) {3 Menuinflater inflater = getmenuinflater (); 4 inflater.inflate (r.menu.game_menu, menu); 5 return true ; 6 }There are two ways to add menu items: One is to reference an XML file, and the other is to use the Add () method.
Android2.3, the system calls the Oncreateoptionmenu () method after the user taps the menu key, and at 3.0 and above, the system calls the Oncreateoptionmenu () method when the activity is started.
Handling menu Item Response events:
Call the onoptionsitemselected(MenuItem Item) method
1 @Override2 Public Booleanonoptionsitemselected (MenuItem item) {3 //Handle Item Selection4 Switch(Item.getitemid ()) {5 CaseR.id.new_game:6 newgame ();7 return true;8 CaseR.id.help:9 showHelp ();Ten return true; One default: A return Super. onoptionsitemselected (item); - } -}
Changing the contents of the Options menu dynamically requires a method that passes the menu onPrepareOptionsMenu() object so that we can manipulate it, such as adding menu items, deleting menus, and so on. Note that at 3.0 and above, you need to call the invalidateoptionsmenu () method before using the Onprepareoptionsmenu method.
Android Learning Note--menu (i)