Android Siege Lion Optionsmenu

Source: Internet
Author: User

There are three types of menus in Android: Optionsmenu Select Menu ContextMenu Context Menu submenu submenu create Options menu: Rewrite Oncreateoptionmenu () in Activity and set menu items inside. The Settings menu item can be added dynamically in code: Menuadd (); It can also be configured via XML (Configure menu items inside) and then set by Menuinflater.inflate (). Then set the menu's Click event to override Onoptionsitemselected (). , is the code for Main.xml under the menu folder. --------------rewrite Oncreateoptionmenu ():p ublic boolean Oncreateoptionsmenu (Menu menu) {   getmenuinflater (). Inflate (R.menu.main, menu);   return true;} --------------from a review area starting with Android3.0, Android does not require a menu to be available on mobile devices. Unlike the desktop menu, Android menus are not visible by default, so Android recommends using Actionbar instead of menus.


1  Public classMainactivityextendsactionbaractivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.fragment_main);7 8     }9 Ten @Override One      Public BooleanOncreateoptionsmenu (Menu menu) { A  -         //inflate the menu; This adds items to the action bar if it is present. -         //XML loading: the         //getmenuinflater (). Inflate (R.menu.main, menu); -         //setting menu items Dynamically -MenuItem item = menu.add (1, 100, 1, "menu One"); -Item.settitle ("AAA"); +Item.seticon (R.drawable.ic_launcher);//api>=11 It does not show icons -Menu.add (1, 101, 1, "menu two"); +Menu.add (1, 102, 1, "menu three"); A  at         return true; -     } -  -     //Create a Click event - @Override -      Public Booleanonoptionsitemselected (MenuItem item) { in         //TODO auto-generated Method Stub -         Switch(Item.getitemid ()) { to         //XML mode: +         //Case R.id.action_menu_item1: -         //Toast.maketext (Mainactivity.this, "click menu One", 0). Show (); the         //Break ; *         // $         //Case R.ID.ACTION_MENU_ITEM2:Panax Notoginseng         //Toast.maketext (Mainactivity.this, "click Menu II", 0). Show (); -         //Break ; the         //To set menu items dynamically: +          Case100: AIntent Intent =NewIntent (mainactivity. This, Sencondactivity.class); the item.setintent (intent); +Toast.maketext (mainactivity. This, "Click menu One", 0). Show (); -              Break; $          Case101: $Toast.maketext (mainactivity. This, "Click Menu Two", 0). Show (); -              Break; -          Case102: theToast.maketext (mainactivity. This, "Click menu Three", 0). Show (); -              Break;Wuyi  the         } -         return Super. onoptionsitemselected (item); Wu     } -  About}

Android Siege Lion Optionsmenu

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.