Menu Resource
2016-04-05 12:40
435 查看
A menu resource defines an application menu (Options Menu, Context Menu, or submenu) that can be inflated with
For a guide to using menus, see the Menus developer guide.
FILE LOCATION:
The filename will be used as the resource ID.COMPILED RESOURCE DATATYPE:Resource pointer to a
In XML:
ELEMENTS:
attributes:
attributes:
its only parameter, which indicates the item clicked. This method takes precedence over the standard callback to
See the example 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 the functionality.
Introduced in API Level 11.
See the Adding the App Bar training class for more information.
Introduced in API Level 11.
See Action Views and Action Providers for more information.
Introduced in API Level 11.
use as the action view. For example,
an action view.
See Action Views and Action Providers for more 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 the functionality.
Introduced in API Level 11.
use in place of the action item. For example,
See Action Views and Action Providers for more 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 the functionality.
Introduced in API Level 14.
which define the item's priority. Valid values:
attributes:
which define the group's priority. Valid values:
The following application code inflates the menu from the
and also declares the on-click callback for two of the items:
}[/code]
Note: The
菜单资源定义应用程序菜单(选项菜单,右键菜单,或子菜单),可以用充气
对于使用指南菜单,看到菜单 开发指南。
文件位置:
文件名 将被用作资源ID。编译的资源数据类型:资源指针
在XML:
内容:
属性:
属性:
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定的方法,因为它可以打破的功能。
介绍了API等级11。
请参阅添加应用栏 培训班以获取更多信息。
介绍了API等级11。
见行动的意见和行动提供更多的信息。
介绍了API等级11。
见行动的意见和行动提供更多的信息。
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定类,因为它可以打破的功能。
介绍了API等级11。
见行动的意见和行动提供更多的信息。
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定类,因为它可以打破的功能。
介绍了API等级14。
属性:
下面的应用程序代码从膨胀菜单
}[/code]
注:该
MenuInflater.
For a guide to using menus, see the Menus developer guide.
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.filenameSYNTAX:
<?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" | "collapseActionView"] android:actionLayout="@[package:]layout/layout_resource_name" android:actionViewClass="class name" android:actionProviderClass="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:
<menu>Required. This must be the root node. Contains
<item>and/or
<group>elements.
attributes:
xmlns:androidXML namespace. Required. Defines the XML namespace, which must 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:idResource 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 new ID.
android:titleString resource. The menu title as a string resource or raw string.
android:titleCondensedString resource. A condensed title as a string resource or a raw string. This title is used for situations in which the normal title is too long.
android:iconDrawable resource. An image to be used as the menu item icon.
android:onClickMethod name. The method to call when this menu item is clicked. The method must be declared in the activity as public and accept a
MenuItemas
its only parameter, which indicates the item clicked. This method takes precedence over the standard callback to
onOptionsItemSelected().
See the example 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 the functionality.
Introduced in API Level 11.
android:showAsActionKeyword. When and how this item should appear as an action item in the app bar. A menu item can appear as an action item only when the activity includes an app bar. Valid values:
Value | Description |
---|---|
ifRoom | Only place this item in the app bar if there is room for it. If there is not room for all the items marked "ifRoom", the items with the lowest orderInCategoryvalues are displayed as actions, and the remaining items are displayed in the overflow menu. |
withText | Also include the title text (defined by android:title) with the action item. You can include this value along with one of the others as a flag set, by separating them with a pipe |. |
never | Never place this item in the app bar. Instead, list the item in the app bar's overflow menu. |
always | Always place this item in the app bar. Avoid using this unless it's critical that the item always appear in the action bar. Setting multiple items to always appear as action items can result in them overlapping with other UI in the app bar. |
collapseActionView | The action view associated with this action item (as declared by android:actionLayoutor android:actionViewClass) is collapsible. Introduced in API Level 14. |
Introduced in API Level 11.
android:actionLayoutLayout resource. A layout to use as the action view.
See Action Views and Action Providers for more information.
Introduced in API Level 11.
android:actionViewClassClass name. A fully-qualified class name for the
Viewto
use as the action view. For example,
"android.widget.SearchView"to use
SearchViewas
an action view.
See Action Views and Action Providers for more 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 the functionality.
Introduced in API Level 11.
android:actionProviderClassClass name. A fully-qualified class name for the
ActionProviderto
use in place of the action item. For example,
"android.widget.ShareActionProvider"to use
ShareActionProvider.
See Action Views and Action Providers for more 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 the functionality.
Introduced in API Level 14.
android:alphabeticShortcutChar. A character for the alphabetic shortcut key.
android:numericShortcutInteger. A number for the numeric shortcut key.
android:checkableBoolean. "true" if the item is checkable.
android:checkedBoolean. "true" if the item is checked by default.
android:visibleBoolean. "true" if the item is visible by default.
android:enabledBoolean. "true" if the item is enabled by default.
android:menuCategoryKeyword. Value corresponding to
Menu
CATEGORY_*constants,
which define the item's priority. Valid values:
Value | Description |
---|---|
container | For items that are part of a container. |
system | For items that are provided by the system. |
secondary | For items that are user-supplied secondary (infrequently used) options. |
alternative | For items that are alternative actions on the data that is currently displayed. |
android:orderInCategoryInteger. 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 are visible, enabled, or checkable). Contains one or more
<item>elements. Must be a child of a
<menu>element.
attributes:
android:idResource 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 new ID.
android:checkableBehaviorKeyword. 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 radio buttons) |
android:visibleBoolean. "true" if the group is visible.
android:enabledBoolean. "true" if the group is enabled.
android:menuCategoryKeyword. Value corresponding to
Menu
CATEGORY_*constants,
which define the group's priority. Valid values:
Value | Description |
---|---|
container | For groups that are part of a container. |
system | For groups that are provided by the system. |
secondary | For groups that are user-supplied secondary (infrequently used) options. |
alternative | For groups that are alternative actions on the data that is currently displayed. |
android:orderInCategoryInteger. 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-click callback 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 [code]onOptionsItemSelected()
}[/code]
Note: The
android:showAsActionattribute is available only on Android 3.0 (API Level 11) and greater.
菜单资源定义应用程序菜单(选项菜单,右键菜单,或子菜单),可以用充气
MenuInflater。
对于使用指南菜单,看到菜单 开发指南。
文件位置:
RES /菜单/ 文件名 的.xml
文件名 将被用作资源ID。编译的资源数据类型:资源指针
菜单(或子类)的资源。资源引用:在Java:
。R.menu 名
在XML:
@ [ 包:]菜单中的文件名句法:
<?xml的 名 “ 机器人:actionProviderClass = ” 级
内容:
<菜单>必选项。这必须是根节点。包含
的<item>和/或
<集团>元素。
属性:
的xmlns:机器人XML命名空间。必需。定义XML命名空间,它必须是
“http://schemas.android.com/apk/res/android”。
<项目>的菜单项。可能包含一个
<菜单>元素(子菜单)。必须是一个的子
<菜单>或
<集团>元素。
属性:
机器人:ID资源ID。一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:
“@ + ID /名称 ”。加符号表示,本应作为一个新的ID来创建。
机器人:标题字符串资源。菜单标题字符串资源或原始字符串。
机器人:titleCondensed字符串资源。缩合的标题作为一个字符串资源或原始字符串。这个标题,用于在其中正常标题过长的情况。
安卓图标绘制资源。图像以用作菜单项图标。
安卓的onClick方法名称。点击该菜单项时要调用的方法。该方法必须在活动中为公共申报,接受一个
菜单项作为其唯一的参数,它表示点击的项目。此法优先于标准的回调
onOptionsItemSelected()。看到在底部的例子。
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定的方法,因为它可以打破的功能。
介绍了API等级11。
机器人:showAsAction关键字。当这个项目应该如何出现在应用栏的操作项。菜单项可以出现仅在活动包括应用栏的操作项。有效值:
值 | 描述 |
---|---|
ifRoom | 只有把这个项目中的应用栏,如果有空间的。如果没有空间,所有标记的项目“ifRoom”,用最低的项目 orderInCategory值显示为行动,其余项目都显示在溢出菜单。 |
withText | 还包括标题文本(定义为安卓标题)的行动项目。您可以包括与别人设置一个标志之一,沿着这条价值,通过与管道分离 |。 |
决不 | 从来没有在应用栏放置此项目。相反,列出应用栏的溢出菜单项。 |
总是 | 始终把这个项目中应用栏。避免使用此,除非它的关键,该项目始终显示在操作栏。设置多个项目始终显示为行动项目可能会导致它们在应用栏其他UI重叠。 |
collapseActionView | 与此行动项目相关的(由所申报的操作观点:actionLayout机器人或 机器人:actionViewClass)是可折叠的, 在API级别14引入。 |
介绍了API等级11。
机器人:actionLayout布局资源。布局为动作视图中使用。
见行动的意见和行动提供更多的信息。
介绍了API等级11。
机器人:actionViewClass类名。对于一个完全限定类名
观作为操作视图中使用。例如,
“android.widget.SearchView”使用
搜索查看作为一个动作视图。
见行动的意见和行动提供更多的信息。
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定类,因为它可以打破的功能。
介绍了API等级11。
机器人:actionProviderClass类名。对于一个完全限定类名
ActionProvider动作到位的项目使用。例如,
“android.widget.ShareActionProvider”使用
ShareActionProvider。
见行动的意见和行动提供更多的信息。
警告:如果您使用混淆你的代码的ProGuard(或类似工具),一定要排除你从重命名该属性指定类,因为它可以打破的功能。
介绍了API等级14。
机器人:alphabeticShortcut字符。字符为字母快捷键。
机器人:numericShortcut整。许多有关数字快捷键。
机器人:可勾选布尔。“真”如果该项目是可检查的。
安卓:检查布尔。“真”,如果该项目被默认选中。
机器人:可见布尔。“真”,如果该项目在默认情况下可见。
机器人:启用布尔。“真”,如果该项目是默认启用的。
机器人:menuCategory关键字。值对应
菜单
CATEGORY_ *常量,它定义了项目的优先级。有效值:
值 | 描述 |
---|---|
容器 | 对于作为容器的一部分的项目。 |
系统 | 对于由该系统提供的物品。 |
次要 | 对于那些用户提供辅助(不常用)选项的项目。 |
替代 | 对于那些在当前显示的数据替代行动项目。 |
机器人:orderInCategory整。的项目的“重要性”,在组内的顺序。
<集团>菜单组(创建共享的特征,比如他们是否可见,启用后,即可检查项目的集合)。包含一个或多个
的<item>元素。必须是一个的子
<菜单>元素。
属性:
机器人:ID资源ID。一个独特的资源ID。要为这个项目创建一个新的资源ID,使用以下形式:
“@ + ID /名称 ”。加符号表示,本应作为一个新的ID来创建。
机器人:checkableBehavior关键字。的类型的该组可检查的行为。有效值:
值 | 描述 |
---|---|
没有 | 没有可检查 |
所有 | 所有项目可以检查(使用复选框) |
单 | 只有一个项目可以检查(使用单选按钮) |
机器人:可见布尔。“真”,如果基团是可见的。
机器人:启用布尔。“真”如果启用该组。
机器人:menuCategory关键字。值对应
菜单
CATEGORY_ *常量,它定义组的优先级。有效值:
值 | 描述 |
---|---|
容器 | 对于作为容器的一部分基团。 |
系统 | 对于由该系统提供的基团。 |
次要 | 对于那些用户提供辅助(不常用)选项组。 |
替代 | 对于群体是在当前显示的数据替代行动。 |
机器人:orderInCategory整。该类别内的项目的默认顺序。例:在保存XML文件
RES /菜单/ 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>
下面的应用程序代码从膨胀菜单
onCreateOptionsMenu(菜单)回调,并声明上单击回调两个项目:
public boolean onCreateOptionsMenu ( Menu menu ) { MenuInflater inflater = getMenuInflater (); inflater . inflate ( R . menu . example_menu , menu ); return true ; } public void onGroupItemClick ( MenuItem item ) { //其中一个组项目(使用onclick属性)被点击 //这里传递的项目参数表明它是哪个项目 //其他所有菜单项点击被处理[code]onOptionsItemSelected()
}[/code]
注:该
机器人:showAsAction属性仅适用于Android 3.0(API等级11)和更大。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件