never:不以button的形式在ActionBar显示出来。
ifRoom:如果系统觉得ActionBar上有足够的空间就以button的形式显示,没有空间就不以button形式显示。
always:总是以button的形式在ActionBar上显示。以往的经验来看,一次显示最好不要超过两个。
withText:总是以文字来显示,不管有没有设置icon。
collapseActionView: item的action view被折叠到一个按钮中,当用户选择这个按钮时,这个操作视窗展开。
可以通过 item 的”android:orderInCategory “属性来进行Menu item的重新排序
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- These are in reverse order in this resource, but the orderInCategory attribute will
order them for the menu (they all have the same default category). -->
<item android:title="forth item"
android:orderInCategory="3"/>
<item android:title="third item"
android:orderInCategory="2"/>
<item android:title="second item"
android:orderInCategory="1"/>
<item android:title="first item"
android:orderInCategory="0"/>
</menu>
在Android3.0及以上的版本上,当出现一个事件并且你想要执行menu的更新动作,可以通过调用“invalidateOptionsMenu() ”来请求系统重新调用“onPrepareOptionsMenu()”从而来更换MenuItem。
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_share:
shareCurrentItem();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
someView.setOnLongClickListener(new View.OnLongClickListener() {
// Called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// Start the CAB using the ActionMode.Callback defined above
mActionMode = getActivity().startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
// Here you can do something when items are selected/de-selected,
// such as update the title in the CAB
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// Respond to clicks on the actions in the CAB
switch (item.getItemId()) {
case R.id.menu_delete:
deleteSelectedItems();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// Inflate the menu for the CAB
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// Here you can make any necessary updates to the activity when
// the CAB is removed. By default, selected items are deselected/unchecked.
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// Here you can perform updates to the CAB due to
// an invalidate() request
return false;
}
});
Menu group是Menu items的组合具有相同的特性。 通过Menu Groups可以做以下事务:
- 使用 setGroupVisible() 显示或隐藏所有项目
- 使用 setGroupEnabled() 启用或禁用所有项目
- 使用 setGroupCheckable() 指定所有项目是否可选中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_save"
android:icon="@drawable/menu_save"
android:title="@string/menu_save" />
<!-- menu group -->
<group android:id="@+id/group_delete">
<item android:id="@+id/menu_archive"
android:title="@string/menu_archive" />
<item android:id="@+id/menu_delete"
android:title="@string/menu_delete" />
</group>
</menu>
Checkable items 只可以在submenus或者context menus中使用。
要使用menu的checkable功能,可以通过设置item的android:checkable或者设置一个group的android:checkableBehavior值,android:checkableBehavior有三个值可以选择:single(单选)、all(多选)、none(不能选)。
当点击了选择menu item之后,需要自己手动写代码来改变item的选择状态,因为系统不会自动修改并保存。如果想要持久化,可以使用Shared Preferences。
<item android:title="All">
<menu>
<group android:id="@+id/checkable_group"
android:checkableBehavior="all">
<item android:id="@+id/checkable_item_1"
android:title="@string/item_1"/>
<item android:id="@+id/checkable_item_2"
android:title="@string/item_2"
android:checked="true"/>
<item android:id="@+id/checkable_item_3"
android:title="@string/item_3"
android:checked="true"/>
</group>
</menu>
</item>