The menu resource is also an XML file, defined in the Res/menu/main.xml directory, referenced with R.menu.main
A typical menu resource file structure is:<menu>, in which the <menu> elements can also be nested in nested <item> and <group> sub-element,<item>.
<menu> There are no attributes in the root element, it contains <item> and <group> elements
<group> represents a menu group, the same properties can be set in a menu group
The common properties of group are as follows:
ID: The reference ID that uniquely identifies the menu group.
Menucategory: Categorize menus, define menu priorities, valid values are container, system, secondary, and altemative.
Orderincategory: A categorical sorted integer.
Checkablebehavior: Choose the behavior, single, multiple, or other. Valid values are none, all, and single.
Visible: TRUE or FALSE.
Enabled: Whether it is available, true or false.
<item> represents a menu item that contains valid properties in <menu> or <group>.
The properties of the <item> element are described below:
ID: The ID reference of the uniquely labeled menu.
Menucategory: Menu category.
or by Rlncategory: Sort by category.
Title: the menu title string.
titlecondensed: Condensed title, suitable for use when the title is too long.
Icon: Icons for the menu.
Alphabeticshortcut: The character shortcut key.
Numericshortcut: Numeric shortcut keys.
Checkable: Optional.
Checked: Whether it has been selected.
Visible: can be seen.
Enabled: is available.
Use of menu Resources
in the menu directory, open the appropriate. xml file, and add the following code
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= " Http://schemas.android.com/tools " tools:context=" com.example.new1.MainActivity " > <item android:title= "File" > <menu> <group android:id= "@+id/file_group" android:checkablebehavior= "None" > <item android:id= "@+id/newfile" android:title= "New" &nbsP;android:alphabeticshortcut= "n" > <!-- add shortcut keys--> </item> <item android:id= "@+id/openfile" android:title= "Open" android:alphabeticshortcut= "O" > </item> &nbsP; <item android:id= "@+id/closefile" android:title= "Close" android: Alphabeticshortcut= "C" > </item> </group> </menu> </ item> <item android:title= "Edit" android:icon= "@drawable/pwordpress" > <!--settings icon --> <menu> <group android:id= "@+id/edit_group" android:checkablebehavior= "single" > <item android:id= "@+id/cut" Android:title= "Cut" android:alphabeticshortcut= "T" > </item> <item android:id= "@+id/copy" android:title= "Copy" android: Alphabeticshortcut= "P" > </item> <item android:id= "@+id/paste" android:title= "Paste" android:alphabeticshortcut= "a" > </item> </group> </menu> </item> <item android:title= "About" android:icon= "@ Drawable/pyahoo "> <menu> <group android:id= "@+id/about_group" > <item Android:id= "@+id/help" &Nbsp;android:title= "Help" > </item> <item android:id= "@+id/exit" android:title= "Exit" > </item> </group> </menu> </iTem></menu>
In the. Java reference, implement the function public in. java boolean oncreatoptionsmenu (menu) function Display Menu
public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it is present.get Menuinflater (). Inflate (R.menu.main, menu); return true;}
Overriding the public boolean onoptionsitemselected (MenuItem item) function in. Java corresponding Menu event
public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.copy) {return true;} return super.onoptionsitemselected (item);}
This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1614840
Android Resource Details (ii) menu resources