Android Study Notes (34): Android menu
Android menus are divided into three types: Option Menu, Context Menu, and Sub Menu ).
Option Menu: Click the Menu button on the device to display the Menu. You cannot select a flag.
Context Menu: a Menu that appears when a component is long pressed. Menu item icons are not supported.
SubMenu: SubMenu. Menu item icons are not supported, and nested sub-menus are not supported.
The Menu class is used to add Menu items and sub-menus by adding () 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 sub menu in the groudId Group
SubMenu addSubMenu (int groupId, int itemId, int order, int titleRes): Add a sub menu in the groudId Group
SubMenu addSubMenu (CharSequence title): Add a sub menu
SubMenu addSubMenu (int titleRes): Add a sub menu
(1) how to create the option menu:
1. Override the onCreateOptionsMenu (Menu menu) method of the Activity.
2. Call the add () method of Menu to add a Menu item (MenuItem). You can call the setIcon () method of MenuItem to set the Menu item icon.
3. Override the onOptionsMenuSelected () method of Acitivy to set event listening for the menu.
(2) how to create a sub-menu:
1. Override the onCreateOptionsMenu () method of the Activity and call the addSubMenu () method of the Menu to add a sub Menu.
2. Call the add () method of SubMenu to add sub-menu items.
3. Override the onCreateItemSelected () method and set listening events for the menu.
The SubMenu class also provides additional methods to set menu header information:
SubMenu setHeaderIcon (Drawable icon): Set the icon of the menu header.
SubMenu setHeaderIcon (int iconRes): Set the menu header icon.
SubMenu setHeaderTitle (CharSequence title): Set the title of the menu header.
SubMenu setHeaderTitle (int titleRes): Set 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. Override the onCreateContextMenu (ContextMenu, View, ContextMenu. ContextMenuInfo) method of the Activity. The View parameter sets the component that triggers the context menu.
2. Call the add () method of Menu to add the Menu item MenuItem.
3. Override the onContextItemSelected () method and set event listening for the context menu.
4. Call the registerForContextMenu (View) method to register the context menu for the View component.
Similarly, we can use setHeaderIcon () and setHeaderTitle () Methods to set icons and titles for context menus.
(4) set event listening for menu items:
We override the onCreateItemSelected () method of Activity to set event listening. Generally, we use the switch method to determine which menu item is clicked. Therefore, we need to set an id for each menu item, to facilitate judgment.
Of course, another method is to bind a listener to each menu item and use the setOnMenuItemClickListener () method. However, this method is very troublesome and is not recommended.
(5) set single-choice menu items and multiple menu items:
SetCheckable (boolean checkable): sets whether the menu item can be checked.
SetGroupCheckable (int group, boolean checkable, boolean exclusive): sets whether the menu items of the group can be checked. If exclu is set to true, this group is a single-choice menu item.
(6) set shortcut keys for menu items (it seems useless now ):
SetAlphabeticShortcut (char alphaChar): sets the letter shortcut key.
SetNumericShortcut (char numericChar): sets the numeric shortcut.
SetShortcut (char numericChar, char alphaChar): set two shortcut keys at the same time.
(7) set the menu items associated with the Activity:
Click a menu item to 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 under the menu file directory under the res file.
2. Use the inflate (int menuRes, Menu menu) method to load the corresponding resources in the onCreateOptionsMenu () method of the Activity or onCreateContextMenu () method.
3. Rewrite the event listening method related to the Activity.
The xml in the menu folder has the following rules:
1. The root element is , No redundant attributes.
2. Add multiple menu items Package it into a menu group.
You can set the following attributes:
CheckableBehavior: Option Selection Behavior, none (not available), all (multiple choice), single (single choice ).
MenuCategory: specify the priority of menu items, including container, system, secondary, and alternative.
Visible: visible
Enable: available or not
3. Defines menu items, which can contain Sub-menu.
You can set the following attributes:
Android: id: Set the ID
Android: title: Set the title
Android: icon: Set icon
Android: alphabeticShortcut: Set the letter shortcut
Android: numbericShortcut: Set the numeric shortcut
Android: checkable: Optional
Android: checked: whether to select
Android: visible
Android: enable: available
(9) pop-up menu PopupMenu
The procedure is as follows:
1. new PopupMenu (Context context, View anchor) creates a pop-up menu. The anchor parameter indicates the component that triggers the menu.
2. Call the inflate () method of MenuInflater to load menu resources.
3. Call the show () method of PopupMenu to display the pop-up menu.