How to define menus in XML _android

Source: Internet
Author: User

As with the Android UI layout, we can also define the application's menus in XML. By expanding the menu layout in the Oncreateoptionsmenu method of the menu. Doing so will make our program code much simpler, and as much as possible put more of the interface design parts into XML for easy browsing.

1. Create a menu folder under the/res/folder of the project to hold the menus XML file you defined for your application.
In the menu XML layout, there are three valid elements: menu, group, item. Item and group must be child elements of the menu, and item must be a child of group. The other menu can be a child of item (in order to create a submenu). The following XML fragment shows the hierarchy definition of the menu:

<?xml version= "1.0" encoding= "Utf-8"?> <menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:title= "Play" android:id= "@+id/media_play" android:icon= "@android:d rawable/ic_media_play" > <item android:title= "Pause" android:id= "@+id/media_pause" android:icon= "@android:d rawable/ic_media_pause" > <item android:title= "File" android:id= "@+id/file" > <menu> <item android:title= "Open ..." android: Id= "@+id/file_open"/> <item android:title= "Save" android:id= "@+id/file_save"/> <item "android:title=" Save as "android:id=" @+id/file_saveas "/> <item android:title=" Exit "android:id=" @+id/file_exit "/> </menu > </item> <item android:title= "Edit" android:id= "@+id/edit" > <menu> <group> <item Android:title= "Copy" android:id= "@+id/edit_copy"/> <item android:title= "Paste" android:id= "@+id/edit_paste"/ > <item android:title= "Cut" android:id= "@+id/edit_cut"/> <item android:title= "Delete "android:id=" @+id/edit_delete "/> </group> </menu> </item> </menu>
 

2. Rewrite the Oncreateoptionsmenu method of the activity to expand the menu XML through the Menuinflater.inflate method.
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.menu_option, menu);

3. Handle the Click events for each menu item in the Onoptionsitemselected method of the activity:

 @Override Public
Boolean onoptionsitemselected (MenuItem item) {
super.onoptionsitemselected (item);
Switch (Item.getitemid ()) {case
r.id.media_play: Break      
;
Case R.id.media_pause: Break      
;
Case R.id.file_open: Break      
;
Case R.id.file_save: ...
}
return true;
}

In XML you can define more features such as icons, shortcuts, and checkbox for menu items, and read more about the topics in the SDK for menus.
The effect of the demo is as shown in figure:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.