Simple app for menu Optionsmenu in Android

Source: Internet
Author: User

Optionsmenu is the menu option in the Android phone

First, to implement the menu operation is to first override the Oncreateoptionsmenu (Menu menu) method

There are usually two ways to implement the addition of options in a menu

The first is dynamic adding: Calling the Add method directly in the Oncreateoptionsmenu method

public boolean Oncreateoptionsmenu (Menu menu) {        menu.add (1,100,1, "1");        Menu.add (1,101,1, "Menu 2");        Menu.add (1,102,1, "menu 3");        Menu.add (1,103,1, "menu 4");        Menu.add (1,104,1, "menu 5");        return true;    }

The second is by means of an XML file

Start by creating a new XML file in the menu file under the Res folder (the default is no menu file under the Res folder in Android Studio, just create a new one yourself)

Then write out the layout to

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/menu_ Item1 "        android:orderincategory=" android:showasaction= "        never"        android:title= "menu One" ></item >    <item        android:id= "@+id/menu_item2"        android:orderincategory= "        android:showasaction=" "Never"        android:title= "Menu II" ></item></menu>

At the same time, if you implement the menu option in an XML file, you will call this layout in the Oncreateoptionsmenu method

public boolean Oncreateoptionsmenu (Menu menu) {        getmenuinflater (). Inflate (R.menu.main,menu);              return true;    }

The last is the menu listener method onoptionsitemselected (MenuItem Item)

@Override Public    Boolean onoptionsitemselected (MenuItem item) {               return super.onoptionsitemselected (item);    }}

Simple app for menu Optionsmenu in Android

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.