Android menu details (5)-use XML to generate menus

Source: Internet
Author: User

Looking back at the previous articles, we add menu items directly to the code and group menu items. This is a traditional practice and it has some shortcomings. For example, to respond to each menu item, we need to use constants to save the ID of each menu item. Therefore, Android provides a better way, that isDefine menu as the application resource.With Android's local support for resources, we can easily create and respond to menus. This article describes how to use an XML file to load and respond to menus. We need to take these steps:

  1. Create a menu folder under the/RES directory
  2. Use the menu-related elements in the menu directory to define the XML file. The file name is random and Android will automatically generate a resource ID for it. For example:R. Menu. mainmenuCorresponds to the mainmenu. xml resource file in the menu directory.
  3. Use the resource ID of the XML file to add the menu items defined in the XML file to the menu object.
  4. When responding to a menu item, use the resource ID corresponding to each menu item

The following describes the Options menu in Android menu (2)-create and respond to options menu in XML.

Define menu resource files

Create a menu folder under the res directory and create an XML resource file under the menu, which is called mainmenu. xml.

Compile mainmenu. XML as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- group1 -->
<group android:id="@+id/group1">
<item android:id="@+id/mi1"
android:title="item1"/>
<item android:id="@+id/mi2"
android:title="item2"/>
</group>
<!-- group 2 -->
<group android:id="@+id/group2">
<item android:id="@+id/mi3"
android:title="item3"/>
<item android:id="@+id/mi4"
android:title="item4"/>
</group>

</menu>

Here, we simply add four menu items and divide them into two groups. The Android: Title value of the item element can reference the string resource in values.

Use menuinflater to add menu items

Inflater builds a bridge from resource files to objects in AndroidMenuinflater converts the menu XML resource to an object and adds it to the menu object. It can be obtained through getmenuinflater () of the activity. In mainactivity, we override the oncreateoptionsmenu (...) method.

@Override
publicboolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
returntrue;
}
Response menu item

At last, rewrite the onoptionsitemseleted (...) method.

@Override
publicboolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.mi1:
// do sth
break;
case R.id.mi2:
// do sth
break;
case R.id.mi3:
// do sth
break;
case R.id.mi4:
// do sth
break;
}
returntrue;
}

This step maximizes the benefits of using XML to generate menus. Android not only generates a resource ID for the defined XML file, but can also generate an ID automatically for the group and menu item (just like generating an ID for the view defined in the layout ). This wayYou don't have to worry about creating and managing menu item IDs., All for Android!

So far, we have completed a simple demo of "using XML to generate Menus" and realized the advantages of using resource files, so this isRecommended menu creation methods in Android. In fact, all menu items or group operations in the code can be completed in the XML file. The following describes some common functions. (The demos API provided by Google has the most comprehensive example)

More menu resource file functions

1. Resource file implementation sub-menu

The sub-menu is implemented by nesting the menu in the item element.

<Item Android: Title = "system settings">
<Menu>
<Item Android: Id = "@ + ID/mi_display_setting"
Android: Title = "Display Settings"/>
<Item Android: Id = "@ + ID/mi_network_setting"
Android: Title = "Network Settings"/>
<! -- Other menu items -->
</Menu>
</Item>

2. Add icons for menu items

<Item Android: Id = "@ + ID/mi_exit"
Android: Title = "quit"
Android: icon = "@ drawable/exit"/>

3. Set optional menu items

Use Android: checkablebehavior to set an optional policy for a set of menu items. Optional values: None, all, single

<Group Android: Id = "..."
Android: checkablebehavior = "all">
<! -- Menu item -->
</Group>

Use Android: checked to set specific menu items

<item android:id="..."
android:title="sometitle"
android:checked="true"/>

4. Set Menu items to available/unavailable

<item android:id="..."
android:title="sometitle"
android:enabled="false"/>

5. Set visible/invisible menu items

<item android:id="..."
android:title="sometitle"
android:visible="false"/>
Conclusion

This is the last part of the android menu detail series. This series details the tips and precautions for using various commonly used menus in Android, hoping to help you better understand them, we are also looking forward to sharing your experiences in development with you :)

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.