Android ApiDemos example (25): App-& gt; Menu-& gt; Inflate fr

Source: Internet
Author: User

This example shows how to expand Menu items from Menu resources (XML definition. In this example, onCreate uses code to create the Activity interface. The usual method is to use the Layout resource method. If you are interested in learning how to use code to create the UI, you can refer to the onCreate method in this example, but this is not the purpose of this example.
The general procedure for expanding a menu by using an XML menu resource is
1. define menu resources under the/res/menu directory. In this example, ten menu items with different styles are defined: "Title only", "Title and Icon", "Submenu ", "Groups", "Checkable", "Shortcuts", "Order", "Category and Order", "Visible", and "Disabled ".
The simplest Menu resource definition is as follows: only id and title are defined.
<Menu xmlns: android = "http://schemas.android.com/apk/res/android”>
<Item android: id = "@ + id/jump"
Android: title = "@ string/jump"/>
<Item android: id = "@ + id/dive"
Android: title = "@ string/dive"/>
</Menu>
2. Expand Menu resources. Android supports two types of Menu options Menu and Context Menu ). In this example, you can use MenuInflater in the public boolean onCreateOptionsMenu (Menu menu) method to expand Menu resources:
 
[Java]
1. @ Override
2. public boolean onCreateOptionsMenu (Menu menu ){
3. // Hold on to this
4. mMenu = menu;
5.
6. // Inflate the currently selected menu XML resource.
7. MenuInflater inflater = getMenuInflater ();
8. inflater. inflate
9. (sMenuExampleResources [mSpinner. getSelectedItemPosition ()],
10. menu );
11.
12. // Disable the spinner since we 've already created the menu and the user
13. // can no longer pick a different menu XML.
14. mSpinner. setEnabled (false );
15.
16. // Change instructions
17. mInstructionsText. setText (getResources (). getString (
18. R. string. menu_from_xml_instructions_go_back ));
19.
20. return true;
21 .}
 

3. Add an event handling method to the menu.
[Java]
1. @ Override
2. public boolean onOptionsItemSelected (MenuItem item ){
3. switch (item. getItemId ()){
4. case R. id. jump:
5 ....
6. default:
7...
 

 
However, when you select a Menu, the onOptionsItemSelected event is triggered. The parameter item is the selected Menu Item. You can perform operations on the Menu item based on the Menu Id.
In this example, the menu uses 10 different styles, and the basic usage is the same. The difference is that the attributes defined in XML items.
Title only
The simplest menu contains only text descriptions.


 

Title and Icon
The menu contains text and icons. Added the android: icon attribute.
<Item android: id = "@ + id/happy"
Android: title = "Happy"
Android: icon = "@ drawable/stat_happy"/>

 
Sub Menu
Although it is unlikely to use a multi-level Menu on a mobile phone, Android can also support multiple menus if you want to use a sub Menu. This is achieved through the nested Menu Item method:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android”>
<Item android: title = "Normal 1"/>
<Item android: id = "@ + id/submenu"
Android: title = "Emotions">
<Menu>
<Item android: id = "@ + id/happy"
Android: title = "Happy"
Android: icon = "@ drawable/stat_happy"/>

...
</Menu>
</Item>
<Item android: title = "Normal 2"/>
</Menu>


 

Groups
In the Menu definition, related menus can be defined as a group. The Menu items in the same group can be defined with the same attributes. For example, you can use setGroupVisible to set whether all members in the group are visible, setGroupEnabled can be used to check whether all the members in the group are valid. The group is defined by group:
<Group android: id = "@ + id/browser">
<Item android: id = "@ + id/refresh"
Android: title = "@ string/browser_refresh"/>
<Item android: id = "@ + id/bookmark"
Android: title = "@ string/browser_bookmark"/>
</Group>
You can define IDs for a group. For example, you can use
[Java]
1. mMenu. setGroupVisible (R. id. browser, shouldShowBrowser );

Checkable
The Menu Item defined in the group can define the checkable of the group. Items in the group can inherit the checkable attribute of the group. Items not in the group can define their own checkable attribute separately, the menu defining the checkable attribute is displayed in the style of Check button or Radio Button:
Checkable defines four different types of checkableBehavior:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android”>
<! -Checkable items appear only in submenus or context menus.->
<! -Carefully look at the attribute name checkableBehavior on groups,
The attribute name checkable on items. The checkableBehavior encompasses
The number of items that will be checkable within that group.->
<Item android: title = "None">
<Menu>
<! -The none checkableBehavior is default, but we explicitly show it here.->
<Group android: id = "@ + id/noncheckable_group"
Android: checkableBehavior = "none">
<! -Notice how these items inherit from the group.->
<Item android: id = "@ + id/noncheckable_item_1 ″
Android: title = "@ string/item_1"/>
<Item android: id = "@ + id/noncheckable_item_2 ″
Android: title = "@ string/item_2"/>
<Item android: id = "@ + id/noncheckable_item_3 ″
Android: title = "@ string/item_3"/>
</Group>
</Menu>
</Item>
<Item android: title = "All">
<Menu>
<Group android: id = "@ + id/checkable_group"
Android: checkableBehavior = "all">
<! -Notice how these items inherit from the group.->
<Item android: id = "@ + id/checkable_item_1 ″
Android: title = "@ string/item_1"/>
<Item android: id = "@ + id/checkable_item_2 ″
Android: title = "@ string/item_2 ″
Android: checked = "true"/>
<Item android: id = "@ + id/checkable_item_3 ″
Android: title = "@ string/item_3 ″
Android: checked = "true"/>
</Group>
</Menu>
</Item>
<Item android: title = "Single">
<Menu>
<Group android: id = "@ + id/exclusive_checkable_group"
Android: checkableBehavior = "single">
<! -Notice how these items inherit from the group.->
<Item android: id = "@ + id/exclusive_checkable_item_1 ″
Android: title = "@ string/item_1"/>
<Item android: id = "@ + id/exclusive_checkable_item_2 ″
Android: title = "@ string/item_2"/>
<Item android: id = "@ + id/exclusive_checkable_item_3 ″
Android: title = "@ string/item_3 ″
Android: checked = "true"/>
</Group>
</Menu>
</Item>
<Item android: title = "All without group">
<Menu>
<! -Notice how these items have each set.->
<Item android: id = "@ + id/nongroup_checkable_item_1 ″
Android: title = "@ string/item_1 ″
Android: checkable = "true"/>
<Item android: id = "@ + id/nongroup_checkable_item_2 ″
Android: title = "@ string/item_2 ″
Android: checkable = "true"
Android: checked = "true"/>
<Item android: id = "@ + id/nongroup_checkable_item_3 ″
Android: title = "@ string/item_3 ″
Android: checkable = "true"
Android: checked = "true"/>
</Menu>
</Item>
</Menu>
 
Shortcuts
You can add shortcut key properties to a menu item, such as pressing the "I" key to trigger the menu item invisible_item.
<Item android: id = "@ + id/invisible_item"
Android: visible = "false"
Android: alphabeticShortcut = "I"
Android: title = "Invisible item"/>
<Item android: id = "@ + id/h_item"
Android: alphabeticShortcut = "h"
Android: title = "Henry"/>

 

When a menu is displayed, because there are More than 6 menu options, h_item is displayed only when you press More. However, h_item defines a shortcut key "h", and then press "h" directly ", the h_item menu item is triggered immediately.
Order
The default display method is the same as the defined order. However, you can use the android: orderInCategory attribute to redefine the menu display attributes. For example, if the attributes defined below are fouth, third, second, first, and orderInCategory are 3, 2, 1, and 0, the actual menu display sequence is as follows:

 
Category and Order
In addition to the default category, you can also define other secondary Category. Each Category can define their android: orderInCategory for each Menu Item separately. The display order of the Menu items is as follows: display All menu items of the default Category in the order of orderInCategory, and then display the menu items in the secondary Category in the order of orderInCategory: for example, category_order.xml is defined as follows:
<Menu xmlns: android = "http://schemas.android.com/apk/res/android”>
<! -This group uses the default category.->
<Group android: id = "@ + id/most_used_items">
<Item android: id = "@ + id/last_most_item"
Android: orderInCategory = "10 ″
Android: title = "@ string/last_most_often"/>
<Item android: id = "@ + id/middle_most_item"
Android: orderInCategory = "7 ″
Android: title = "@ string/middle_most_often"/>
<Item android: id = "@ + id/first_most_item"
Android: orderInCategory = "4 ″
Android: title = "@ string/first_most_often"/>
</Group>
<Group android: id = "@ + id/least_used_items"
Android: menuCategory = "secondary">
<Item android: id = "@ + id/last_least_item"
Android: orderInCategory = "3 ″
Android: title = "@ string/last_least_often"/>
<Item android: id = "@ + id/middle_least_item"
Android: orderInCategory = "2 ″
Android: title = "@ string/middle_least_often"/>
<Item android: id = "@ + id/first_least_item"
Android: orderInCategory = "0 ″
Android: title = "@ string/first_least_often"/>
</Group>
</Menu>
The last menu display order is as follows:
Visible
MenuItem has an android: visible attribute. The default value is true. If it is defined as "false", this menu item is not displayed even if it is defined in XML.
<Item android: id = "@ + id/visible_item"
Android: title = "Visible"
Android: alphabeticShortcut = "a"/>
<Item android: id = "@ + id/hidden_item"
Android: title = "Hidden"
Android: visible = "false"
Android: alphabeticShortcut = "B"/>
As defined above, only the visible menu items are displayed:
 
Disabled
In addition to the visable attribute, you can also specify the enabled attribute. The default value of the enabled attribute is true, which indicates that the menu item is valid. If the value is false, the menu item is invalid and is usually displayed in gray.
<Item android: id = "@ + id/enabled_item"
Android: title = "Enabled"
Android: icon = "@ drawable/stat_happy"/>
<Item android: id = "@ + id/disabled_item"
Android: title = "Disabled"
Android: enabled = "false"

Android: icon = "@ drawable/stat_sad"/>
 

Author: mapdigit
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.