Книга: Beginning Android

Menu XML Structure

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 their own filename and the .xml extension.

For example, from the Menus/Inflation sample project at http://apress.com/, 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>

Note the following about menu XML definitions:

• You must start with a menu root element.

• Inside a menu are item elements and group elements, the latter representing a collection of menu items that can be operated upon as a group.

• Sub-menus 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 sub-menu.

• 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.

Оглавление книги

Оглавление статьи/книги

Генерация: 0.496. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз