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