Inside a menu element are item elements and group elements.. Menu Options and XML Inside the item and group elements, you can specify various options, matching up with corresponding met
Trang 1Yet More Inflation
Chapter 8 explained how you can describe Views via XML files and “inflate” them into
actual View objects at runtime Android also allows you to describe menus via XML files
and inflate them when a menu is needed This helps you keep your menu structure
separate from the implementation of menu-handling logic, and it provides easier ways to
develop menu-authoring tools
Menu XML Structure
Menu XML goes in res/menu/ in your project tree, alongside the other types of resources
that your project might employ As with layouts, you can have several menu XML files in
your project, each with its own filename and the xml extension
For example, from the Menus/Inflation sample project, here is a menu called
sample.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/close"
android:title="Close"
android:orderInCategory="3"
android:icon="@drawable/eject" />
<item android:id="@+id/no_icon"
android:orderInCategory="2"
android:title="Sans Icon" />
<item android:id="@+id/disabled"
android:orderInCategory="4"
android:enabled="false"
android:title="Disabled" />
<group android:id="@+id/other_stuff"
android:menuCategory="secondary"
android:visible="false">
<item android:id="@+id/later"
android:orderInCategory="0"
android:title="2nd-To-Last" />
<item android:id="@+id/last"
android:orderInCategory="1"
android:title="Last" />
</group>
<item android:id="@+id/submenu"
android:orderInCategory="3"
android:title="A Submenu">
<menu>
<item android:id="@+id/non_ghost"
android:title="Non-Ghost"
android:visible="true"
android:alphabeticShortcut="n" />
<item android:id="@+id/ghost"
android:title="A Ghost"
android:visible="false"
android:alphabeticShortcut="g" />
</menu>
</item>
</menu>
Trang 2Note the following about the XML for menus:
You must start with a menu root element
Inside a menu element are item elements and group elements The latter represents a collection of menu items that can be operated upon
as a group
Submenus are specified by adding a menu element as a child of an item element, using this new menu element to describe the contents of the submenu
If you want to detect when an item is chosen, or to reference an item
or group from your Java code, be sure to apply an android:id, just as you do with View layout XML
Menu Options and XML
Inside the item and group elements, you can specify various options, matching up with corresponding methods on Menu or MenuItem, as follows:
Title: The title of a menu item is provided via the android:title
attribute on an item element This can be either a literal string or a reference to a string resource (e.g., @string/foo)
Icon: Menu items optionally have icons To provide an icon, in the form
of a reference to a drawable resource (e.g., @drawable/eject), use the android:icon attribute on the item element
Order: By default, the order of the items in the menu is determined by
the order in which they appear in the menu XML You can change that
by specifying the android:orderInCategory attribute on the item element This is a 0-based index of the order for the items associated with the current category There is an implicit default category Groups can provide an android:menuCategory attribute to specify a different category to use for items in that group Generally, it is simplest just to put the items in the XML in the order you want them to appear
Enabled: Items and groups can be enabled or disabled, controlled in
the XML via the android:enabled attribute on the item or group element By default, items and groups are enabled Disabled items and groups appear in the menu but cannot be selected You can change
an item’s status at runtime via the setEnabled() method on MenuItem,
or change a group’s status via setGroupEnabled() on Menu
Trang 3Visible: Items and groups can be visible or invisible, controlled in the
XML via the android:visible attribute on the item or group element
By default, items and groups are visible Invisible items and groups do
not appear in the menu You can change an item’s status at runtime
via the setVisible() method on MenuItem, or change a group’s status
via setGroupVisible() on Menu In the layout XML shown in the
previous section, the other_stuff group is initially invisible If we make
it visible in our Java code, the two menu items in the group will
“magically” appear
Shortcut: Items can have shortcuts—single letters
(android:alphabeticShortcut) or numbers (android:numericShortcut)
that can be pressed to choose the item without needing to use the
touchscreen, D-pad, or trackball to navigate the full menu
Inflating the Menu
Actually using the menu, once it’s defined in XML, is easy Just create a MenuInflater
and tell it to inflate your menu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
theMenu=menu;
new MenuInflater(getApplication())
.inflate(R.menu.sample, menu);
return(super.onCreateOptionsMenu(menu));
}