showAsAction參數
never:不以button的形式在ActionBar顯示出來。
ifRoom:如果系統覺得ActionBar上有足夠的空間就以button的形式顯示,沒有空間就不以button形式顯示。
always:總是以button的形式在ActionBar上顯示。以往的經驗來看,一次顯示最好不要超過兩個。
withText:總是以文字來顯示,不管有沒有設置icon。
collapseActionView: item的action view被摺疊到一個按鈕中,當用戶選擇這個按鈕時,這個操作視窗展開。
menu排序
可以通過 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>
在運行中修改menu items
在Android3.0及以上的版本上,當出現一個事件並且你想要執行menu的更新動作,可以通過調用“invalidateOptionsMenu() ”來請求系統重新調用“onPrepareOptionsMenu()”從而來更換MenuItem。
Using the contextual action mode
爲單個視圖啓用上下文操作模式
- 實現 ActionMode.Callback 接口。在這個callback方法中,你可以指定 Icontextual action bar的響應動作, 響應每一個action items的click事件,並且控制action mode的其他他生命週期事件。
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;
}
};
- 當你想要顯示ActionMode bar的時候(比如長按View),調用 startActionMode() 。
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 或 GridView 中啓用批處理上下文操作
- 實現 AbsListView.MultiChoiceModeListener 接口,並使用 setMultiChoiceModeListener() 爲視圖組設置該接口。在偵聽器的回調方法中,您既可以爲上下文操作欄指定操作,也可以響應操作項目的點擊事件,還可以處理從 ActionMode.Callback 接口繼承的其他回調。
- 使用 CHOICE_MODE_MULTIPLE_MODAL 參數調用 setChoiceMode()。
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 Groups
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>