Android has three different forms of menus:
1. Options menu (Optinosmenu)
2. Context menu (ContextMenu)
3. Sub-menu (submenu)
The most common of these is the Options menu (Optionsmenu), which is displayed at the bottom of the corresponding activity when you click the menu button.
1.Activity menu Mechanism(similar to Dialog)
Activity has a mechanism for managing menus, as follows:
1.public boolean Oncreateoptionsmenu (Menu menu)此方法用于初始化菜单,其中menu参数就是即将要显示的Menu实例。
返回true则显示该menu,false 则不显示;(只会在第一次初始化菜单时调用)
2.public boolean onPrepareOptionsMenu(Menu menu)在Oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is displayed.
The same, 返回true则显示该menu,false 则不显示;(可以通过此方法动态的改变菜单的状态,比如加载不同的菜单等)
3.public void onOptionsMenuClosed(Menu menu)每次菜单被关闭时调用.
The menu is closed in three situations where menu button is clicked again, the Back button is clicked, or the user selects one of the menus.
The 4.public boolean onoptionsitemselected (MenuItem Item) menu item is invoked when clicked, that is, the method of monitoring the item.
2. Add a menu:
can be in Oncreateoptionsmenu oronPrepareOptionsMenu方法中来添加菜单
2.1 Code Additions:
Menu.add (int groupId, int itemId, int order, charsequence title). SetIcon (drawable ID)
The four parameters of the Add () method are, in turn:
1, groups, if not grouped, then write Menu.none,
2, ID, this is very important, Android based on this ID to determine the different menu
3, order, which menu item in front by the size of this parameter is determined
4, Text, menu item display text
The Add () method returns a MenuItem object, calls its SetIcon () method, sets the icon for the corresponding MenuItem
Public Boolean Oncreateoptionsmenu (Menu menu) { super. Oncreateoptionsmenu (menu); Menu.add (Menu.none, Menu.first+1, 0, "Settings"). SetIcon (r.drawable.setting); return true ; }
2.2 Layout File Additions:
Getmenuinflater (). Inflate (R.menu.options_menu, menu);
Call the activity's Getmenuinflater () to get a menuinflater, using the inflate method to load the defined menu in the layout file to the menus object for the second parameter
@Override publicboolean oncreateoptionsmenu (Menu menu) { Super. Oncreateoptionsmenu (menu); Getmenuinflater (). Inflate (R.menu.options_menu, menu); return true ; }
Layout file: Create a menu folder in the Res directory, creating a layout file: Options_menu.xml
<?XML version= "1.0" encoding= "Utf-8"?><Menuxmlns:android= "Http://schemas.android.com/apk/res/android"> <ItemAndroid:id= "@+id/menu_setting"Android:title= "Settings"Android:icon= "@drawable/setting"></Item></Menu>
3. Menu item Monitoring:
Onoptionsitemselected (MenuItem Item) is triggered whenever a menu item in the menu is clicked
The item parameter is the menu item that is clicked, so it is necessary to determine which item is clicked in this method, thus implementing different actions.
For the two different ways to add a menu, the method of judging is a little different, but the essence is the same.
3.1 How to determine the code to add a menu
@Override Public Booleanonoptionsitemselected (MenuItem item) {Super. onoptionsitemselected (item); Switch(Item.getitemid ())//get the Itemid of the item clicked { CaseMenu.first+1://The corresponding ID is the ID set in the Add method Break; CaseMenu.first+2: Break; } return true; }
3.2 How to determine the layout file Add menu:
@Override Public Booleanonoptionsitemselected (MenuItem item) {Super. onoptionsitemselected (item); Switch(Item.getitemid ())//get the Itemid of the item clicked { CaseR.id.menu_setting://The ID here is the ID defined in the layout file, which is obtained using the R.id.xxx method . Break; CaseR.id.menu_info: Break; } return true; }
Android Oncreateoptionsmenu () Create Menus menu