您的位置:首页 > 移动开发 > Android开发

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
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.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" | "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:android
XML 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: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 new ID.
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. This title 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. The method must be declared in the activity as public and accept a 
MenuItem
 as
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:showAsAction
Keyword. 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:
ValueDescription
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 
orderInCategory
 values 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:actionLayout
 or 
android:actionViewClass
) is collapsible.

Introduced in API Level 14.
See the Adding the App Bar training class for more information.
Introduced in API Level 11.
android:actionLayout
Layout resource. A layout to use as the action view.
See Action Views and Action Providers for more information.
Introduced in API Level 11.
android:actionViewClass
Class name. A fully-qualified class name for the 
View
 to
use as the action view. For example,
"android.widget.SearchView"
 to use 
SearchView
 as
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:actionProviderClass
Class name. A fully-qualified class name for the 
ActionProvider
 to
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: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:
ValueDescription
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: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 are visible, enabled, or checkable). Contains one or more 
<item>
 elements. Must be a child 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 new ID.
android:checkableBehavior
Keyword. The type of checkable behavior for the group. Valid values:
ValueDescription
none
Not checkable
all
All items can be checked (use checkboxes)
single
Only one item can be checked (use radio buttons)
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:
ValueDescription
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: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-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:showAsAction
 attribute 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)和更大。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息