Android Settings button: Long Press or click the menu key
1. Long press OPTION:
Layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen Activity_vertical_margin "tools:context=". Menuactivity "> <textview android:id=" @+id/menutext "android:layout_width=" Fill_parent "Android:layout_height" = "Wrap_content" android:text= "First method Create Menu" android:textsize= "sp"/> <textview android:id= "@+id/menutext" Android : layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "the second way to create a menu" android:textsize= "SP"/ > <listview android:id= "@+id/menulist" android:layout_width= fill_parent "android:layout_height=" fill_parent "Android:layout_margintop=" DP > </ListView> </LinearLayout>
Implementation procedure:
Private ListView list;
Private TextView LV;
string[] str = {"TextView", "EditView", "Toast (message box)"}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_menu);
List = (ListView) Findviewbyid (r.id.menulist); arrayadapter<string> arr = new Arrayadapter<string> (this, Android.
R.layout.simple_list_item_, str);
List.setadapter (arr); List.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {@Override public void Oncreatecontextmenu ( ContextMenu menu, View V, contextmenuinfo menuinfo) {menu.clear (); Menu.clearheader (); Menu.setheadericon (
R.drawable.ic_launcher));
Menu.setheadertitle ("I Am ListView");
Menu.add (,,, "delete");
Menu.add (,,, "modify");
}
});
The first method TextView TV = (TextView) Findviewbyid (R.id.menutext);
THIS.REGISTERFORCONTEXTMENU (TV);
The second method of LV = (TextView) Findviewbyid (R.id.menutext); Lv.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {@Override public void OnCReatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {menu.setheadertitle ("length-contextmenu"); menu.add
(,,, "copy");
submenu SB = Menu.addsubmenu ("find");
Sb.add (,,, "Find by id");
Sb.add (,,, "Search by name");
}
}); }
2. Long by menu item:
Private ListView list;
Private TextView LV;
string[] str = {"TextView", "EditView", "Toast (message box)"}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_menu);
List = (ListView) Findviewbyid (r.id.menulist); arrayadapter<string> arr = new Arrayadapter<string> (this, Android.
R.layout.simple_list_item_, str);
List.setadapter (arr); List.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {@Override public void Oncreatecontextmenu ( ContextMenu menu, View V, contextmenuinfo menuinfo) {menu.clear (); Menu.clearheader (); Menu.setheadericon (
R.drawable.ic_launcher));
Menu.setheadertitle ("I Am ListView");
Menu.add (,,, "delete");
Menu.add (,,, "modify");
}
});
The first method TextView TV = (TextView) Findviewbyid (R.id.menutext);
THIS.REGISTERFORCONTEXTMENU (TV);
The second method of LV = (TextView) Findviewbyid (R.id.menutext); Lv.setoncreatecontextmenulistener (New Oncreatecontextmenulistener () {@Override public void OnCReatecontextmenu (ContextMenu menu, View V, contextmenuinfo menuinfo) {menu.setheadertitle ("length-contextmenu"); menu.add
(,,, "copy");
submenu SB = Menu.addsubmenu ("find");
Sb.add (,,, "Find by id");
Sb.add (,,, "Search by name");
}
}); }
Here's how to add some basic menu usage
Using XML to define Menu
The menu resource file must be placed in the Res/menu directory. The menu resource file must use the <menu> tag as the root node. In addition to the <menu> tags, there are two additional tabs for setting up menu items and groupings, both <item> and <group>.
The <menu> label does not have any properties, but can be nested in the <item> tab to represent the form of a submenu. However, the <item> tag can no longer embed <item> tags.
The attribute meaning of the 1.<item> label is as follows:
ID: The resource ID that represents the menu item
Menucategory: The same type of vegetable item. This property is preferable to 4 values: container, System, secondary, and alternative. You can control the position of a menu item by using the Menucategroy property. For example, setting the property to system means that the menu item is a system menu and should be placed behind other kinds of menu items.
Orderincategor: The order of the same kind of menu. This property requires an integer value to be set. For example, the Menucategory property values are 3 menu items for System (ITEM1, ITEM2, and Item3). Set the Orderincategory property value of these 3 menu items to 3, 2, 1, then Item3 will appear at the front, and Item1 will appear at the end.
Title: Menu item Header (text displayed for the item)
Titlecondensed: A short caption for a menu item. This property value is displayed when the menu item header is too long
Icon: Menu item icons Resource ID
Alphabeticshortcut: Letter shortcuts for menu items
Numericshortcut: Number shortcut keys for menu items
Checkable: Indicates whether the menu item has a check box. This property can be designed to true or false
Checked: If the menu item has a check box (the Checkable property is true), this property indicates whether the default state of the check box is selected. can set a value of TRUE or False
Visible: Menu item default state is visible
Enable: Whether the menu item default state is activated
The attribute meaning of the 2.<group> label is as follows:
ID: Represents the ID of a menu group
Menucategory: The same name as the <item> label has the same attribute meaning. Only the scope is a menu group
Orderincategory: The same name as the <item> label has the same attribute meaning. Only the scope is a menu group
Checkablebehavior: Sets the selection component (checkbox or Radio Button) displayed on all menu items for this group. If the property value is set to all, display
checkbox component; If set to single, displays the radio button component; If set to none, displays the normal menu item (no selection component appears). Note that the official version of the Android SDK document has a clerical error when interpreting the attribute:
Whether the items are checkable. Valid Values:none, All (Exclusive/radiobuttons), single (non-exclusive/checkboxes).
On the contrary, the right should be
All (Non-exclusive/checkboxes), single (exclusive/radiobuttons).
Visible: Indicates whether all menu items in the current group are displayed. The property can be set to a value of true or False
Enable: Indicates whether all menu items in the current group are active. The property can be set to a value of true or False