First, using XML to define the menu
The menu resource file must be placed in the Res/menu directory. The menu resource file must use the <menu> tag as the root node. In addition to the <menu> tags, there are two other tabs for setting menu items and grouping, both <item> and <group>.
The <menu> tag does not have any attributes, but can be nested in the <item> tag, representing the form of a submenu. However, the <item> tag can no longer embed <item> tags.
The properties of the 1.<item> tag have the following meanings:
ID: The resource ID that represents the menu item
Menucategory: The type of the same vegetable item. This property has 4 values: container, System, secondary, and alternative. You can control the location of menu items by using the Menucategroy property. For example, setting a property to system means that the menu item is a systems menu and should be placed behind other kinds of menu items.
Orderincategor: The sort order of the same kind of menu. This property requires an integer value to be set. For example, the Menucategory property value is 3 menu items for System (ITEM1, ITEM2, and Item3). Set the Orderincategory property value of the 3 menu items to 3, 2, 1, then Item3 will appear first, and Item1 will appear in the last face.
Title: Menu item Title (the text displayed by the menubar)
Titlecondensed: Short title of the menu item. This property value is displayed when the menu item title is too long
Icon: Menu item icon resource ID
Alphabeticshortcut: The letter shortcut key for a menu item
Numericshortcut: Numeric shortcut keys for menu items
Checkable: Indicates whether the menu item has a check box. This property can be designed to be true or false
Checked: If the menu item has a check box (the Checkable property is true), this property indicates whether the check box default state is selected. A value that can be set to TRUE or False
Visible: Whether the default state of a menu item is visible
Enable: Whether the menu item default state is activated
The properties of the 2.<group> tag have the following meanings:
ID: ID representing the menu group
Menucategory: The same name attribute as the <item> tag has the same meaning. Just scoped to a menu group
Orderincategory: The same name attribute as the <item> tag has the same meaning. Just scoped to a menu group
Checkablebehavior: Sets the selection component (checkbox or Radio Button) that appears on all menu items in the group. If this property value is set to all, the checkbox component is displayed, if set to single, the radio button component is displayed, and if none is set, the normal menu item is displayed (no selection component is displayed). Note that the official documentation for the Android SDK has a clerical error explaining this property, and the original text is:
Whether the items are checkable. Valid Values:none, All (Exclusive/radiobuttons), single (non-exclusive/checkboxes).
On the contrary, the right should be
All (Non-exclusive/checkboxes), single (exclusive/radiobuttons).
Visible: Indicates whether all menu items in the current group are displayed. The property can be set to a value of true or False
Enable: Indicates whether all menu items in the current group are active. The property can be set to a value of true or False
3. Specific Use
Use in code
Public Boolenoncreateoptionsmenu (Menu menu) {
Menuinflatemenuinflate = Getmenuinflate ();
Menuinflate.inflate (R.menu.option_menu,menu);
Returntrue;
}
Ii. using code to define menu
public boolean Oncreateoptionsmenu (Menu menu) {
Method One, build with code
Menu.add (Menu.none, Menu.none, 1, "menu 1");
Menu.add (Menu.none, Menu.none, 2, "Menu 2");
Menu.add (Menu.none, Menu.none, 3, "menu 3");
Menu.add (Menu.none, Menu.none, 4, "menu 4");
Menu.add (Menu.none, Menu.none, 5, "menu 5");
Menu.add (Menu.none, Menu.none, 6, "menu 6");
return true;
}
Third, menu response
Rewrite onoptionsitemselected (MenuItem Item) This method can be done in response to the operation.
Iv. examples
Activity_main.xml files under Res/menu
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <Item Android:id= "@+id/menu_settings"android:orderincategory= "1"Android:icon= "@drawable/Home"Android:title= "@string/menu_settings"/> <Item Android:id= "@+id/menu_about"android:orderincategory= "2"Android:icon= "@drawable/mine"Android:title= "@string/menu_about"/> <Item Android:id= "@+id/menu_quit"android:orderincategory= "3"Android:icon= "@drawable/more"Android:title= "@string/menu_quit"/> </menu>
View Code
Packagecom.example.menutest; ImportAndroid.os.Bundle; Importandroid.app.Activity; ImportAndroid.view.Menu; ImportAndroid.view.MenuItem; ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present. getmenuinflater (). Inflate (R.menu.activity_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//TODO auto-generated Method Stub Switch(Item.getitemid ()) { CaseR.id.menu_about:toast.maketext (mainactivity. This, "" + "about", Toast.length_short). Show (); Break; CaseR.id.menu_settings:toast.maketext (mainactivity. This, "" + "settings", Toast.length_short). Show (); Break; CaseR.id.menu_quit:toast.maketext (mainactivity. This, "" + "Exit", Toast.length_short). Show (); Break; default: Break; } //Toast.maketext (Mainactivity.this, "" +item.getitemid (), Toast.length_short). Show (); return Super. onoptionsitemselected (item); } } View Code
Operation Result:
Basic usage of menu in Android