XML usage of menu, submenu, popupmenu, and contextmenu

Source: Internet
Author: User
Tags addall

Menu is introduced in android3.0. With the XML file of menu, we can quickly create a new menu file. Next we will introduce several commonly used menus one by one.

1. Call menu. Rewrite the two methods to implement it.

@ Override public Boolean oncreateoptionsmenu (menu) {// todo auto-generated method stub getmenuinflater (). inflate (R. menu. main, menu); // fill in the main. XML menu layout return Super. oncreateoptionsmenu (menu) ;}@ override public Boolean onoptionsitemselected (menuitem item) {// monitor the sub-item of the menu. // todo auto-generated method stub switch (item. getitemid () {case R. id. add: Toast. maketext (mainactivity. this, "add", toast. length_short ). show (); break; case R. id. edit: Toast. maketext (mainactivity. this, "edit", toast. length_short ). show (); break; case R. id. DEL: Toast. maketext (mainactivity. this, "Del", toast. length_short ). show (); break;} return Super. onoptionsitemselected (item );}

2. For more information about submenu usage, see the code and comments.

@ Override public Boolean onmenuitemselected (INT featureid, menuitem item) {// rewrite onmenuitemselected to monitor click events of sub-options. During my experiment, it is found that changing the topic will invalidate the sub-option // todo auto-generated method stub switch (item. getitemid () {case R. id. addall: Toast. maketext (mainactivity. this, "addall", toast. length_short ). show (); break; case R. id. addsomeone: Toast. maketext (mainactivity. this, "addsomeone", toast. length_short ). show (); break;} return Super. onmenuitemselected (featureid, item );}

 

3. popmenu usage

Declare in menu XML;

<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" tools: context = "$ {packagename }. $ {activityclass} "><! -- Set the method body for triggering in the button --> <button Android: Id = "@ + ID/button1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_alignparenttop = "true" Android: layout_marginleft = "22dp" Android: layout_margintop = "45dp" Android: onclick = "showpopmenu" Android: TEXT = "pop-up menu"/> <listview Android: Id = "@ + ID/listview1" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: layout_alignparentleft = "true" Android: layout_below = "@ + ID/button1"> </listview> </relativelayout>

Implement the method body in the activity to achieve the pop-up effect

    public void showpopMenu(View view) {        PopupMenu popupMenu = new PopupMenu(context, view);        popupMenu.getMenuInflater().inflate(R.menu.menu1, popupMenu.getMenu());        popupMenu.show();    }

3. contextmenu. Rewrite two methods to achieve the contextmenu effect.

Public class mainactivity extends activity implements onitemlongclicklistener {private listview lv; private arrayadapter <string> marrayadapter; public context = mainactivity. this; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Init (); LV. setadapter (marrayadapter); registerforcontextmenu (LV);} void Init () {Lv = (listview) findviewbyid (R. id. listview1); marrayadapter = new arrayadapter <string> (context, android. r. layout. simple_list_item_1, getdata ();} public void showpopmenu (view) {popupmenu = new popupmenu (context, view); popupmenu. getmenuinflater (). inflate (R. menu. menu1, popupmenu. getmenu (); popupmenu. show () ;}list <string> getdata () {list <string> MList = new arraylist <string> (); For (INT I = 0; I <10; I ++) {MList. add ("minfan" + I);} return MList ;}@ override public Boolean oncontextitemselected (menuitem item) {// todo auto-generated method stub adaptercontextmenuinfo info = (adaptercontextmenuinfo) item. getmenuinfo (); string value = marrayadapter. getitem (info. position); // value indicates the item information in listview, used to locate switch (item. getitemid () {case R. id. item1: Toast. maketext (context, "Item1" + value, toast. length_short ). show (); break; case R. id. item2: Toast. maketext (context, "item2" + value, toast. length_short ). show (); break; case R. id. item3: Toast. maketext (context, "item3" + value, toast. length_short ). show (); break;} return Super. oncontextitemselected (item) ;}@ override public void oncreatecontextmenu (Android. view. contextmenu menu, view V, android. view. contextmenu. contextmenuinfo menuinfo) {getmenuinflater (). inflate (R. menu. menu1, 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.