Menu Resource
See also
- Creating Menus
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) thatcan be inflated with MenuInflater
.
For a guide to using menus, see the CreatingMenus document.
- file location:
-
res/menu/filename.xml
The filename will be used as the resource ID. - compiled resource datatype:
- Resource pointer to a
Menu
(or subclass) resource. - resource reference:
- In Java:
R.menu.filename
In XML: @[package:]menu.filename
- syntax:
-
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:onClick="method name" android:showAsAction=["ifRoom" | "never" | "withText" | "always"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="class name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" /> <group android:id="@[+][package:]id/resource name" android:checkableBehavior=["none" | "all" | "single"] android:visible=["true" | "false"] android:enabled=["true" | "false"] android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" > <item /> </group> <item > <menu> <item /> </menu> </item></menu>
- elements:
-
- Required. This must be the root node. Contains
<item>
and/or <group>
elements. attributes:
-
xmlns:android
- XML namespace. Required. Defines the XML namespace, whichmust be
"http://schemas.android.com/apk/res/android"
.
-
<item>
- A menu item. May contain a
<menu>
element (for a Sub Menu). Must be a child of a <menu>
or <group>
element. attributes:
-
android:id
- Resource ID. A unique resource ID. To create a new resource ID for this item, use the form:
"@+id/name"
. The plus symbol indicates that this should be created as a newID. -
android:title
- String resource. The menu title as a string resource or raw string.
-
android:titleCondensed
- String resource. A condensed title as a string resource or a raw string. Thistitle is used for situations in which the normal title is too long.
-
android:icon
- Drawable resource. An image to be used as the menu item icon.
-
android:onClick
- Method name. The method to call when this menu item is clicked. Themethod must be declared in the activity as public and accept a
MenuItem
as itsonly parameter, which indicates the item clicked. This method takes precedence over the standardcallback to onOptionsItemSelected()
. See theexample at the bottom. Warning: If you obfuscate your code using ProGuard (or a similar tool),be sure to exclude the method you specify in this attribute from renaming, because it can break thefunctionality.
Introduced in API Level 11.
-
android:showAsAction
- Keyword. When and how this item should appear as an action item in the ActionBar. A menu item can appear as an action item only when the activity includes an
ActionBar
(introduced in API Level 11). Valid values: Value | Description |
ifRoom | Only place this item in the Action Bar ifthere is room for it. |
withText | Also include the title text (definedby android:title ) with the action item. You can include this value along with oneof the others as a flag set, by separating them with a pipe | . |
never | Never place this item in the Action Bar. |
always | Always place this item in the Action Bar.Avoid using this unless it's critical that the item always appear in the actionbar. Setting multiple items to always appear as action items can result in them overlappingwith other UI in the action bar. |
See Using the Action Bar formore information.
Introduced in API Level 11.
-
android:actionViewLayout
- Layout resource. A layout to use as the action view.
See Using the Action Bar formore information.
Introduced in API Level 11.
-
android:actionViewClassName
- Class name. A fully-qualified class name for the
View
to use as the action view. See Using the Action Bar formore information.
Warning: If you obfuscate your code using ProGuard (or a similar tool),be sure to exclude the class you specify in this attribute from renaming, because it can break thefunctionality.
Introduced in API Level 11.
-
android:alphabeticShortcut
- Char. A character for the alphabetic shortcut key.
-
android:numericShortcut
- Integer. A number for the numeric shortcut key.
-
android:checkable
- Boolean. "true" if the item is checkable.
-
android:checked
- Boolean. "true" if the item is checked by default.
-
android:visible
- Boolean. "true" if the item is visible by default.
-
android:enabled
- Boolean. "true" if the item is enabled by default.
-
android:menuCategory
- Keyword. Value corresponding to
Menu
CATEGORY_*
constants, which define the item's priority. Valid values: Value | Description |
container | For items that are part of acontainer. |
system | For items that are provided by thesystem. |
secondary | For items that are user-supplied secondary(infrequently used) options. |
alternative | For items that are alternative actionson the data that is currently displayed. |
-
android:orderInCategory
- Integer. The order of "importance" of the item, within a group.
-
<group>
- A menu group (to create a collection of items that share traits, such as whether they arevisible, enabled, or checkable). Contains one or more
<item>
elements. Must be achild of a <menu>
element. attributes:
-
android:id
- Resource ID. A unique resource ID. To create a new resource ID for this item,use the form:
"@+id/name"
. The plus symbol indicates that this should be created as a newID. -
android:checkableBehavior
- Keyword. The type of checkable behavior for the group. Valid values:
Value | Description |
none | Not checkable |
all | All items can be checked (use checkboxes) |
single | Only one item can be checked (use radiobuttons) |
-
android:visible
- Boolean. "true" if the group is visible.
-
android:enabled
- Boolean. "true" if the group is enabled.
-
android:menuCategory
- Keyword. Value corresponding to
Menu
CATEGORY_*
constants, which define the group's priority. Valid values: Value | Description |
container | For groups that are part of acontainer. |
system | For groups that are provided by thesystem. |
secondary | For groups that are user-supplied secondary(infrequently used) options. |
alternative | For groups that are alternative actionson the data that is currently displayed. |
-
android:orderInCategory
- Integer. The default order of the items within the category.
- example:
- XML file saved at
res/menu/example_menu.xml
: <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="@string/item1" android:icon="@drawable/group_item1_icon" android:showAsAction="ifRoom|withText"/> <group android:id="@+id/group"> <item android:id="@+id/group_item1" android:onClick="onGroupItemClick" android:title="@string/group_item1" android:icon="@drawable/group_item1_icon" /> <item android:id="@+id/group_item2" android:onClick="onGroupItemClick" android:title="@string/group_item2" android:icon="@drawable/group_item2_icon" /> </group> <item android:id="@+id/submenu" android:title="@string/submenu_title" android:showAsAction="ifRoom|withText" > <menu> <item android:id="@+id/submenu_item1" android:title="@string/submenu_item1" /> </menu> </item></menu>
The following application code inflates the menu from the onCreateOptionsMenu(Menu)
callback and also declares the on-clickcallback for two of the items:
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true;}public void onGroupItemClick(MenuItem item) { // One of the group items (using the onClick attribute) was clicked // The item parameter passed here indicates which item it is // All other menu item clicks are handled by onOptionsItemSelected()
}
Note: The android:showAsAction
attribute isavailable only on Android 3.0 (API Level 11) and greater.
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer
随机推荐
-
在不可见的webbrowser对象中模拟按键C#
-
jquery将html转换为字符串和html
-
img在div中不按百分比调整大小
-
为什么代码放到DW里运行,和用记事保存为H
-
cordova HTML5第一次尝试
-
jQuery SerialScroll:将类添加到活动项
-
HTml中面试题 ---&&的作用以及||特点
-
C++Test的使用说明
-
HTML H5之ASCII 代码转义字符集实体编号
-
我的HTML INPUT无效标记,请帮忙