【0】先看一段官方的说明:
Menus are an important part of an activity's user interface, which provide users a familiar way to perform actions. Android offers a simple framework for you to add standard menus to your application.
There are three types of application menus:
-
Options Menu
- The primary collection of menu items for an activity, which appears when the user touches the MENU button. When your application is running on Android 3.0 or later, you can provide quick access to select menu items by placing them directly in the Action Bar, as "action items."
- 这个其实就像是这样的菜单:
-
- A floating list of menu items that appears when the user touches and holds a view that's registered to provide a context menu.
- 这个的话其实可以理解成PC上点击右键的菜单:
-
Submenu
- A floating list of menu items that appears when the user touches a menu item that contains a nested menu.
- 这个Submenu就是子菜单的意思,点击一个菜单后进入子菜单,有点像 PC application's menu bar (File, Edit, View, etc.).
-
【1】使用XML文件来创建OptionsMenu
- 这是使用inflater XML的方式来创建一个Menu.显然我们需要在res/menu/的目录下创建类似下面的一个文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
android:icon="@drawable/ic_new_game"
android:title="@string/new_game" />
<item android:id="@+id/help"
android:icon="@drawable/ic_help"
android:title="@string/help" />
</menu>
android:id
android:icon
android:title
- Inflating a Menu Resource
使用MenuInflater来创建一个Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
在3.0之前会默认显示6个items,如果超出6个,最后一个就用more来显示,点击more会显示其余的items.
3.0之前onCreateOptionsMenu是在第一次点击menu的时候被呼叫到,而3.0之后是在activity被创建的时候就被呼叫了
当点击menu按键的时候会call 到onCreateOptionsMenu.
- 当用户点击其中的item的适合会call到下面的方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
- 根据AP运行情况来调整Menu的Item
onCreateOptionsMenu() 的方法只会call到一次,之后一直存在直到activity销毁,如果我们需要在程序运行时根据不同的情况来调整menu的item话,那么需要使用onPrepareOptionsMenu()
,这个方法传入一个已经存在的Menu,之后我们可以进行 remove, add, disable, or enable menu items.
On Android 2.3 and lower, the system calls onCreateOptionsMenu() 的方法只会call到一次,之后一直存在直到activity销毁,如果我们需要在程序运行时根据不同的情况来调整menu的item话,那么需要使用onPrepareOptionsMenu()
,这个方法传入一个已经存在的Menu,之后我们可以进行 remove, add, disable, or enable menu items.
onPrepareOptionsMenu()
each time the user opens the Options Menu.
On Android 3.0 and higher, you must call
【2】创建Context Menu
invalidateOptionsMenu()
when you want to update the menu, because the menu is always open. The system will then callonPrepareOptionsMenu()
so you can update the menu items.
On Android, a context menu is displayed when the user performs a "long press" (press and hold) on an item.
我们需要使用长时间点击某个Item来显示一个Context Menu.通常使用在ListView中比较多。
需要注意的是,我们使用Context Menu之前需要进行注册的动作,如下描述:
In order for a View to provide a context menu, you must "register" the view for a context menu. Call registerForContextMenu()
and pass it the View
you want to give a context menu. When this View then receives a long-press, it displays a context menu.
For example:
registerForContextMenu()
and pass it the View
you want to give a context menu. When this View then receives a long-press, it displays a context menu.
Register a ListView
If your activity uses a
【3】Creating Submenus
ListView
and you want all list items to provide a context menu, register all items for a context menu by passing the ListView
toregisterForContextMenu()
. For example, if you're using a ListActivity
, register all list items like this:
registerForContextMenu(getListView()
);
实现创建Context Menu,我们需要用到onCreateContextMenu()
and onContextItemSelected()
.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
Note: Items in a context menu do not support icons or shortcut keys.
【3】Creating Submenus
onCreateContextMenu()
and onContextItemSelected()
.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
editNote(info.id);
return true;
case R.id.delete:
deleteNote(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
}
Note: Items in a context menu do not support icons or shortcut keys.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file"
android:icon="@drawable/file"
android:title="@string/file" >
<!-- "file" submenu -->
<menu>
<item android:id="@+id/create_new"
android:title="@string/create_new" />
<item android:id="@+id/open"
android:title="@string/open" />
</menu>
</item>
</menu>
When the user selects an item from a submenu, the parent menu's respective on-item-selected callback method receives the event. For instance, if the above menu is applied as an Options Menu, then the
onOptionsItemSelected()
method is called when a submenu item is selected.
【4】Notes
- Menu groups
A menu group is a collection of menu items that share certain traits. With a group, you can:
- Show or hide all items with
setGroupVisible()
- Enable or disable all items with
setGroupEnabled()
- Specify whether all items are checkable with
setGroupCheckable()
You can create a group by nesting <item>
elements inside a <group>
element in your menu resource or by specifying a group ID with the the add()
method.
下面是一个MenuGroups的例子:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:icon="@drawable/item1"
android:title="@string/item1" />
<!-- menu group -->
<group android:id="@+id/group1">
<item android:id="@+id/groupItem1"
android:title="@string/groupItem1" />
<item android:id="@+id/groupItem2"
android:title="@string/groupItem2" />
</group>
</menu>
- Checkable menu items
You can define the checkable behavior for individual menu items using the android:checkable
attribute in the <item>
element, or for an entire group with the android:checkableBehavior
attribute in the <group>
element. For example, all items in this menu group are checkable with a radio button:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/red"
android:title="@string/red" />
<item android:id="@+id/blue"
android:title="@string/blue" />
</group>
</menu>
哎,今天先写到这了,有机会下次再介绍,谢谢!