Settings button in Android: Press and hold or tap the menu key
1. Long Press the option:
Layout file:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context=". Menuactivity " > One A <TextView - Android:id= "@+id/menutext1" - Android:layout_width= "Fill_parent" the Android:layout_height= "Wrap_content" - Android:text= "First method to create menu" - android:textsize= "20SP" /> - + <TextView - Android:id= "@+id/menutext2" + Android:layout_width= "Fill_parent" A Android:layout_height= "Wrap_content" at Android:text= "Second method of creating menu" - android:textsize= "20SP" /> - - <ListView - Android:id= "@+id/menulist" - Android:layout_width= "Fill_parent" in Android:layout_height= "Fill_parent" - Android:layout_margintop= "50DP" > to </ListView> + - </LinearLayout>
Implementation process:
1 PrivateListView list;2 PrivateTextView LV;3 4string[] str = {"TextView", "EditView", "Toast (message box)" };5 6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_menu);Ten OneList =(ListView) Findviewbyid (r.id.menulist); Aarrayadapter<string> arr =NewArrayadapter<string> ( This, - Android. R.layout.simple_list_item_1, str); - List.setadapter (arr); the -List.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () { - - @Override + Public voidOncreatecontextmenu (ContextMenu menu, View V, - contextmenuinfo Menuinfo) { + menu.clear (); A Menu.clearheader (); at Menu.setheadericon ((R.drawable.ic_launcher)); -Menu.setheadertitle ("I Am the ListView"); -Menu.add (3, 6, 0, "delete"); -Menu.add (3, 7, 1, "modify"); - } - }); in //The first of these methods -TextView TV =(TextView) Findviewbyid (R.ID.MENUTEXT1); to This. REGISTERFORCONTEXTMENU (TV); + //The second method of -LV =(TextView) Findviewbyid (R.ID.MENUTEXT2); theLv.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () { * $ @OverridePanax Notoginseng Public voidOncreatecontextmenu (ContextMenu menu, View V, - contextmenuinfo Menuinfo) { theMenu.setheadertitle ("Length-contextmenu"); +Menu.add (0, 1, 0, "copy"); Asubmenu SB = Menu.addsubmenu ("Find"); theSb.add (1, 2, 0, "Find by ID"); +Sb.add (1, 3, 0, "Find by name"); - } $ }); $}
2. Long Press Menu item:
1 PrivateListView list;2 PrivateTextView LV;3 4string[] str = {"TextView", "EditView", "Toast (message box)" };5 6 @Override7 protected voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.activity_menu);Ten OneList =(ListView) Findviewbyid (r.id.menulist); Aarrayadapter<string> arr =NewArrayadapter<string> ( This, - Android. R.layout.simple_list_item_1, str); - List.setadapter (arr); the -List.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () { - - @Override + Public voidOncreatecontextmenu (ContextMenu menu, View V, - contextmenuinfo Menuinfo) { + menu.clear (); A Menu.clearheader (); at Menu.setheadericon ((R.drawable.ic_launcher)); -Menu.setheadertitle ("I Am the ListView"); -Menu.add (3, 6, 0, "delete"); -Menu.add (3, 7, 1, "modify"); - } - }); in //The first of these methods -TextView TV =(TextView) Findviewbyid (R.ID.MENUTEXT1); to This. REGISTERFORCONTEXTMENU (TV); + //The second method of -LV =(TextView) Findviewbyid (R.ID.MENUTEXT2); theLv.setoncreatecontextmenulistener (NewOncreatecontextmenulistener () { * $ @OverridePanax Notoginseng Public voidOncreatecontextmenu (ContextMenu menu, View V, - contextmenuinfo Menuinfo) { theMenu.setheadertitle ("Length-contextmenu"); +Menu.add (0, 1, 0, "copy"); Asubmenu SB = Menu.addsubmenu ("Find"); theSb.add (1, 2, 0, "Find by ID"); +Sb.add (1, 3, 0, "Find by name"); - } $ }); $}
Over
Menu in Android