To add a submenu to the Optionsmenu Options menu:
1 Public classMainactivityextendsActivity {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. - //getmenuinflater (). Inflate (R.menu.main, menu); the - //XML File Loading -Menuinflater Inflater =Getmenuinflater (); - inflater.inflate (R.menu.main, menu); + - //Code Dynamic Add submenu + //submenu file = Menu.addsubmenu ("files"); A //submenu edit = Menu.addsubmenu ("edit"); at // - //File.add (1, 1, 1, "new"); - //File.add (1, 2, 1, "open"); - //File.add (1, 3, 1, "save"); - //file.setheadertitle ("file operation");//Sub-menu title - //File.setheadericon (r.drawable.ic_launcher);//Sub-menu icons in // - //Edit.add (2, 1, 1, "copy"); to //Edit.add (2, 2, 1, "paste"); + //Edit.add (2, 3, 1, "cut"); - //file.setheadertitle ("edit operation"); the //File.setheadericon (r.drawable.ic_launcher); * $ return true;Panax Notoginseng } - the @Override + Public Booleanonoptionsitemselected (MenuItem item) { A //TODO auto-generated Method Stub the //XML loading: + Switch(Item.getitemid ()) { - CaseR.id.new_file: $Toast.maketext (mainactivity. This, "Click New", 0). Show (); $ - Break; - CaseR.id.open_file: theToast.maketext (mainactivity. This, "Click to open", 0). Show (); - Wuyi Break; the CaseR.id.save_file: -Toast.maketext (mainactivity. This, "Click to save", 0). Show (); Wu - Break; About CaseR.id.c_edit: $Toast.maketext (mainactivity. This, "Click to copy", 0). Show (); - - Break; - CaseR.id.v_edit: AToast.maketext (mainactivity. This, "Click to paste", 0). Show (); + the Break; - CaseR.id.x_edit: $Toast.maketext (mainactivity. This, "Click Cut", 0). Show (); the the Break; the } the //Code Load: - //if (item.getgroupid () = = 1) { in //switch (Item.getitemid ()) { the //Case 1: the //Toast.maketext (Mainactivity.this, "click New", 0). Show (); About //Break ; the // the //Case 2: the //Toast.maketext (Mainactivity.this, "Click to open", 0). Show (); + //Break ; - //Case 3: the //Toast.maketext (Mainactivity.this, "Click to save", 0). Show ();Bayi //Break ; the // } the // } - // - //if (item.getgroupid () = = 2) { the //switch (Item.getitemid ()) { the //Case 1: the //Toast.maketext (Mainactivity.this, "Click to copy", 0). Show (); the //Break ; - // the //Case 2: the //Toast.maketext (Mainactivity.this, "Click to paste", 0). Show (); the //Break ;94 //Case 3: the //Toast.maketext (Mainactivity.this, "click Cut", 0). Show (); the //Break ; the //98 // } About // } - return Super. onoptionsitemselected (item);101 }102}
Android Siege Lion Use submenu to create a submenu