The Android menu has these types:
1,optionmenue: Options Menu
2,contextmenu: Context Menu
3,submenu Sub-Menu
Among them, the difference between Optionmenue and ContextMenu (the difference between Optionmenue and submenu is smaller):
1,optionmenue corresponding to four activity, an activity can only have one option menu
2,contextmenu for the view, each view can set the context menu
3, generally contextmenu commonly used in the ListView or the GridView;
The following are described in detail:
First: optionmenue: Options menu,
1): The implementation of the menu:
How to implement in activity: Oncreateoptionsmenu ()
1.1: Set menu item available code dynamically set Menuadd ();
1.2: Via XML settings,Getmenuinflater (). Inflate
2): The implementation of monitoring:
How to implement in activity: Oncreateoptionsmenu ()
Examples are as follows:
@Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); Switch(ID) { Caser.id.action_menu_item1://Set Jump InterfaceIntent Intent =NewIntent (mainactivity. This, Settingactivity.class); Item.setintent (Intent); //Toast.maketext (This, "clicked Menu 1", 0). Show (); Break; CaseR.id.action_menu_item2:toast.maketext ( This, "clicked Menu 2", 0). Show (); Break; } return Super. onoptionsitemselected (item); }
Where Main.xml is implemented as follows:
<menu xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " tools:context=" Com.example.optionmenu.MainActivity "> < Item Android:id= "@+id/action_menu_item1" android:orderincategory= "Android" : Showasaction= "Never" android:title= "Settings"/> <item android:id= "@+ Id/action_menu_item2 " android:orderincategory=" android:showasaction= " Never " android:title=" option "/></menu>
Second: contextMenu: Context menu (Popup under Windows right button, Android for file Long Press):
Composition: Icons, headings, menu items, monitoring
To create a method:
1, in the View Registration context menu Registerforcontextmenu ()
2, add context menu content Oncreatecontextmenu ()
2.1: Can be dynamically added via code
2.2: You can load menu items in an XML folder
3, set menu click Response Event oncontenxtitemselected ();
Example: Mainactivity is implemented as follows:
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Showlistview (); }//Set up a list of impressionsPrivate voidShowlistview () {ListView ListView=(ListView) Findviewbyid (R.id.listview); Arrayadapter<String> adapter =NewArrayadapter<string> ( This, Android. R.layout.simple_list_item_1, GetData ()); Listview.setadapter (adapter); This. Registerforcontextmenu (ListView);//setting Menu Options }
Set up data for a listPrivateArraylist<string>GetData () {ArrayList<String> list =NewArraylist<string>(); for(inti=0;i<15;i++) {List.add ("List" +i); } returnlist; } @Override Public voidOncreatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {Super. Oncreatecontextmenu (menu, V, menuinfo); //setting Menu Display ContentsMenu.setheadertitle ("Action");
Method Two: Set the option through XML Menu.setheadericon (r.drawable.ic_launcher);
Method One: Add dynamic by adding//menu.add (1, 1, 1, "copy");//menu.add (1, 2, 1, "paste");//menu.add (1, 3, 1, "cut");//menu.add (1, 4, 1, "rename");Menuinflater Inflater =Getmenuinflater (); Inflater.inflate (R.menu.main, menu);} @Override Public Booleanoncontextitemselected (MenuItem item) {Switch(Item.getitemid ()) { Case1: Toast.maketext ( This, "Click to copy", 0). Show (); Break; Case2: Toast.maketext ( This, "Click to paste", 0). Show (); Break; Case3: Toast.maketext ( This, "Click Cut", 0). Show (); Break; Case4: Toast.maketext ( This, "Click Rename", 0). Show (); Break; } return Super. oncontextitemselected (item);} }
Third: Submenu submenu:
This implementation is similar to Optionmenue, and the methods needed to implement are consistent, but need to be defined in the Oncreateoptionsmenu () method submenu
Object
Specific examples are as follows:
Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present. //getmenuinflater (). Inflate (R.menu.main, menu);submenu file = Menu.addsubmenu ("Files"); submenu Edit= Menu.addsubmenu ("edit"); File.add (1, 1, 1, "new"); File.add (1, 2, 1, "open"); File.add (1, 3, 1, "Save"); File.setheadertitle ("File Operations"); File.setheadericon (R.drawable.ic_launcher); Edit.add (2, 1, 1, "Copy"); Edit.add (2, 2, 1, "Paste"); Edit.add (2, 3, 1, "cut"); Edit.add (2, 4, 1, "rename"); Edit.setheadertitle ("Edit Action"); Edit.setheadericon (R.drawable.ic_launcher); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml. if(Item.getgroupid () ==1){ Switch(Item.getitemid ()) { Case1: Toast.maketext ( This, "Click New", 0). Show (); Break; Case2: Toast.maketext ( This, "Click to open", 0). Show (); Break; Case3: Toast.maketext ( This, "Click to save", 0). Show (); Break; } }Else if(Item.getgroupid () ==2){ Switch(Item.getitemid ()) { Case1: Toast.maketext ( This, "Click to copy", 0). Show (); Break; Case2: Toast.maketext ( This, "Click to paste", 0). Show (); Break; Case3: Toast.maketext ( This, "Click Cut", 0). Show (); Break; Case4: Toast.maketext ( This, "Click Rename", 0). Show (); Break; } } return Super. onoptionsitemselected (item); }}
Darren
Weibo: @IT_ Siege Division
GitHub: @Darren90
Source: http://www.cnblogs.com/fengtengfei/
Android various Menu usage Introduction