The Android menu is divided into three types: the Options menu, the context menu, and the submenu (Sub menu).
Options Menu: Press the menu button on the Device menu to appear. Tick marks are not supported.
Context Menu: Long press the menu that appears for a component. menu item icons are not supported.
Sub-menu (submenu): is a sub-menu. menu item icons are not supported and nested submenus are not supported.
the menu class is used to add menu items and submenus using Add () and addsubmenu ():
MenuItem Add (charsequence title): Add a menu item
MenuItem Add (int groupId, int itemId, int order, int titleres): Add a menu item in the Groudid group
MenuItem Add (int titleres): Add a menu item
MenuItem Add (int groupId, int itemId, int order, charsequence title): Add a menu item in the Groudid group
submenu Addsubmenu (int groupId, int itemId, int order, charsequence title): Add a submenu in the Groudid group
submenu Addsubmenu (int groupId, int itemId, int order, int titleres): Add a submenu in the Groudid group
submenu Addsubmenu (charsequence title): Add a submenu
submenu Addsubmenu (int titleres): Add a submenu
(1) How to create the Options menu:
1, rewrite the Activity of the oncreateoptionsmenu (Menu menu) method.
2. The add () method of the menu (MenuItem)iscalled , and you can call the MenuItem of the SetIcon () method to set the icon for the menu item.
3.override the acitivy onoptionsmenuselected () method to set the event listener for the menu.
(2) How to create a sub-menu:
1, rewrite the Activity of the oncreateoptionsmenu () method, call the Menu the addsubmenu () method adds a submenu.
2.call the Add () method of submenu , adding a submenu item.
3.override the oncreateitemselected () method to set the Listener event for the menu.
The submenu class also provides an additional way to set the information for the menu header:
submenu Setheadericon (drawable icon): Sets the icon for the menu header.
submenu Setheadericon (int iconres): Sets the icon for the menu header.
submenu Setheadertitle (charsequence title): Sets the title of the menu header.
submenu Setheadertitle (int titleres): Sets the title of the menu header.
submenu Setheaderview (View view): Use the view Component To set the menu header.
(3) How to create a context menu:
1, rewrite the Activity of the oncreatecontextmenu (ContextMenu, View, contextmenu.contextmenuinfo) Method,theView parameter sets the component that triggers the context menu.
2. Add menu item by calling the add () method of MenuItem
3. Override the oncontextitemselected () method to set the event listener for the context menu.
4. Call the registerforcontextmenu (view) method to register the context menu for the view component.
We can also use the Setheadericon (),setheadertitle () method to set the icon and caption for the context menu.
(4) Set the event listener for the menu item:
We rewrite the activity of the oncreateitemselected () method to set the event listener, we usually use switch method determines which menu item is clicked, so we need to set an ID for each item to make it easier to judge.
Of course we have another way of binding listeners for each menu item, using the setonmenuitemclicklistener () method, but doing so is cumbersome and not recommended.
(5) set menu items and multi-select menu items:
Setcheckable (Boolean checkable): Sets whether the menu item can be checked.
setgroupcheckable (int group,boolean Checkable,boolean Exclusive): Sets Whether the Group menu item can be checked if exclu is set to true, the group is a single-selection menu item.
(6) Set shortcut keys for menu items (now it seems useless):
Setalphabeticshortcut (char Alphachar): Sets the letter shortcut key.
Setnumericshortcut (char Numericchar): Sets the numeric shortcut key.
Setshortcut (char Numericchar, char Alphachar): Set two shortcut keys at the same time.
(7) set menu items associated with the Activity:
When you click a menu item, you can jump to another Activity.
Use the setintent (Intent Intent) method.
(8) Create a menu using an XML file:
The creation method is
1. Create an XML file for the menu below the res file under the menus file directory .
2. In the Activity 's oncreateoptionsmenu () method or the oncreatecontextmenu () method the inflate (int menures, menu menu) method loads the corresponding resource.
3. Override activity -related event listener methods.
The XML under the menu folder has the following rules:
1. The root element is <menu.../>, no extra attributes.
2.<group.../> multiple menu items <item.../> packages into a single menu group.
You can set the following properties:
Checkablebehavior: Menu Item Amount selection behavior,none(not selectable), all(multiple selection) , single (radio).
Menucategory: Specifies the priority of the menu item,container,system,Secondary and alternative.
Visible: See
Enable: Is available
3.<item.../> is used to define menu items, which can include <menu.../>inside, which is a submenu.
The following properties can be set:
Android:id: Set ID
Android:title: Setting the title
Android:icon: Set icon
Android:alphabeticshortcut: Set letter shortcut Keys
Android:numbericshortcut: Setting a numeric shortcut key
Android:checkable: Optional
Android:checked: Whether to select
Android:visible: whether visible
Android:enable: Is available
(9) Pop-up menu PopupMenu
The steps to create are as follows:
1. New PopupMenu (Context Context,view anchor) creates a pop-up menu, andtheanchor parameter represents the component that triggers the menu.
2. Call menuinflater 's inflate () method to load the menu resource.
3. Call the Show () method of PopupMenu to display the pop-up menu.
Android Learning Note: Android menu