Details the Menu menu key in Android
- 2021-06-29 11:56:32
- OfStack
Setup Button in Android: Long Press or Click Menu Key
1. Long press options:
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:orientation="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=" No. 1 Method to create menus "
android:textSize="sp" />
<TextView
android:id="@+id/menutext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" No. 2 Method to create menus "
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 process:
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 ");
}
});
// No. 1 Method
TextView tv = (TextView) findViewById(R.id.menutext);
this.registerForContextMenu(tv);
// No. 2 Method
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(" lookup ");
sb.add(, , , " Press id lookup ");
sb.add(, , , " Find by name ");
}
});
}
2.Long press 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 ");
}
});
// No. 1 Method
TextView tv = (TextView) findViewById(R.id.menutext);
this.registerForContextMenu(tv);
// No. 2 Method
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(" lookup ");
sb.add(, , , " Press id lookup ");
sb.add(, , , " Find by name ");
}
});
}
Here are some basic usages of Menu
Defining Menu using xml
Menu resource files must be placed in the res/menu directory.Menu resource files must be used
<
menu
>
Label as root node.except
<
menu
>
In addition to tags, there are two other tags for setting menu items and grouping. These two tags are
<
item
>
and
<
group
>
.
<
menu
>
Labels have no attributes, but can be nested within
<
item
>
Label, which represents the form of a submenu.However
<
item
>
Labels can no longer be embedded
<
item
>
Label.
1.
<
item
>
The attributes of a tag mean the following:
Id: Resource ID representing menu item
menuCategory: A category of the same menu item.This property can take four values: container, system, secondary, and alternative.The menuCategroy property allows you to control the location of menu items.For example, setting the property to system means that the menu item is a system menu and should be placed after other types of menu items.
orderInCategor: The order in which menus of the same type are listed.This property requires an integer value to be set.For example, the menuCategory attribute values are system's three menu items (item1, item2, and item3).Set the orderInCategory attribute values of the three menu items to 3, 2, and 1, then item3 will appear at the top and item1 at the bottom.
title: Menu item title (text displayed by menu item)
titleCondensed: Short title of menu item.This property value is displayed when the menu item title is too long
icon: Menu Icon Resource ID
alphabeticShortcut: Letter Shortcut for Menu Items
numericShortcut: Number shortcut for menu item
checkable: Indicates whether a menu item has a check box.This property can be designed as true or false
checked: If a menu item has a check box (the checkable property is true), this property indicates whether the default state of the check box is selected.Settable values are true or false
visible: Is the default state of menu items visible
enable: Is the default state of menu items activated
2.
<
group
>
The attributes of a tag mean the following:
id: ID representing a menu group
menuCategory: and
<
item
>
Tags have the same name attribute meaning.Scope is menu group only
orderInCategory: and<
item
>
Tags have the same name attribute meaning.Scope is menu group only
checkableBehavior: Sets the selection component (CheckBox or Radio Button) displayed on all menu items in the group.If this property value is set to all, display
CheckBox components;If set to single, show the Radio Button component;If set to none, normal menu items are displayed (no selection components are shown).It is important to note that the official document Android SDK interprets this property with a mistake in the original text:
Whether the items are checkable. Valid values: none, all(exclusive/radiobuttons), single(non-exclusive/checkboxes).
Instead, the correct answer should be
all(non-exclusive/checkboxes),single(exclusive/radiobuttons).
visible: Indicates whether all menu items in the current group are displayed.The value that this property can set is true or false
enable: Indicates whether all menu items in the current group are activated.The value that this property can set is true or false