Android Oncreateoptionsmenu () Create menu

Source: Internet
Author: User

Android has three different forms of menus:
1. Options menu (Optinosmenu)
2. Context menu (ContextMenu)
3. Sub-menu (submenu)
The most common of these is the Options menu (Optionsmenu), which is displayed at the bottom of the corresponding activity when you click the menu button.
1.Activity menu mechanism (similar to dialog)
activity has a mechanism for managing menus, as follows:

 Public classMainactivity extends Activity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  Setcontentview (R.layout.activity_main); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {/** * This method is used to initialize the menu, where the menus parameter is an instance of the menu that will be displayed.     Returns true to show that the menu,false is not displayed; * (only called when the menu is initialized for the first time) inflate the menus;     This adds items to the Action Bar * if it is present. */getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Publicboolean Onprepareoptionsmenu (Menu menu) {/** * After the oncreateoptionsmenu is executed, the menu is called before it is displayed, and if the menu is already created, it is called before the menu is displayed. Similarly, * Returns true to show that the menu,false is not displayed; (This method can dynamically change the state of a menu, such as loading different menus, etc.) TODO * auto-generated method stub*/    returnsuper.onprepareoptionsmenu (menu); } @Override Public voidonoptionsmenuclosed (Menu menu) {/** * Each time the menu is closed, it is called. (menu is closed in three scenarios, the Menus button is clicked again, the Back button is clicked, or a user selects a menu item) TODO * auto-generated method stub*/super.onoptionsmenuclosed (menu); } @Override Publicboolean onoptionsitemselected (MenuItem item) {/** * The menu item is called when clicked, that is, the method of listening for the food item. * With these methods, you can be informed that for activity, only one menu object can be displayed and monitored at the same time. TODO auto-generated * Method Stub*/    returnsuper.onoptionsitemselected (item); }}

2. Add a menu:
can be added in Oncreateoptionsmenu or onPrepareOptionsMenu方法中来添加菜单
2.1 code:

  Menu.add (intintint order, charsequence title). SetIcon (drawable ID)

The four parameters of the Add () method are, in turn:

1, groups, if not grouped, then write Menu.none,

2, ID, this is very important, Android based on this ID to determine the different menu

3, order, which menu item in front by the size of this parameter is determined

4, Text, menu item display text

The Add () method returns a MenuItem object, calls its SetIcon () method, sets the icon for the corresponding MenuItem
Example:

 Public boolean Oncreateoptionsmenu (Menu menu) {        Super.oncreateoptionsmenu (menu);        Menu.add (Menu.none,  menu.first+10" settings "). SetIcon ( r.drawable.setting);         return true ;    

2.2 Layout File Additions:

Call activity's Getmenuinflater () to get a menuinflater,

@Override      Public boolean Oncreateoptionsmenu (Menu menu) {        Super.oncreateoptionsmenu (menu);        Getmenuinflater (). Inflate (R.menu.options_menu, menu);         return true ;    

Layout file:
set up a menu folder in the Res directory and create a layout file: Options_menu.xml

<?xml version="1.0" encoding="utf-8"?> <menu    xmlns:android="http://schemas.android.com/apk/res/android">    <item android : id="" android:title=" set " android:icon= "  @drawable/setting"></item>    

  3. Menu item Listener:
    triggers onoptionsitemselected (MenuItem item)
    item parameter is the menu item that is clicked, so it is necessary to determine which item is clicked in this method, thus implementing different actions.
    for two different ways to add a menu, The method of judging is a little different, but the essence is the same.
    3.1 Code Add menu method
Use the Inflate method to load the defined menu in the layout file to The second parameter corresponds to the menu object
        Example:

@Override Publicboolean onoptionsitemselected (MenuItem item) {super.onoptionsitemselected (item); Switch(Item.getitemid ())//get the Itemid of the item clicked        {         Casemenu.first+1://The corresponding ID is the ID set in the Add method             Break;  Casemenu.first+2 :             Break; }        return true; } 

3.2 How to determine the layout file Add menu:

@Override Publicboolean onoptionsitemselected (MenuItem item) {super.onoptionsitemselected (item); Switch(Item.getitemid ())//get the Itemid of the item clicked        {         CaseR.id.menu_setting://The ID here is the ID defined in the layout file, which is obtained using the R.id.xxx method .             Break;  CaseR.id.menu_info: Break; }        return true; }

Android Oncreateoptionsmenu () Create menu

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.