contextual action mode

来源:互联网 发布:安卓上的windows模拟器 编辑:程序博客网 时间:2024/06/03 15:44

在Google的开发文档的guide的menu里面,提到上下文菜单的两种形式。


There are two ways to provide contextual actions:

  • In a floating context menu. A menu appears as a floating list of menu items (similar to a dialog) when the user performs a long-click (press and hold) on a view that declares support for a context menu. Users can perform a contextual action on one item at a time.
  • In the contextual action mode. This mode is a system implementation of ActionMode that displays a contextual action bar at the top of the screen with action items that affect the selected item(s). When this mode is active, users can perform an action on multiple items at once (if your app allows it).
一种是:浮动的上下文菜单,这也是我们经常用到的。

一种是:contextual action mode

效果图如下:



contextual action mode即是右边的那幅图。

我实现的效果如下图:



  1. Implement the ActionMode.Callback interface. In its callback methods, you can specify the actions for the contextual action bar, respond to click events on action items, and handle other lifecycle events for the action mode.
  2. Call startActionMode() when you want to show the bar (such as when the user long-clicks the view).
也就是说第一步需要实现ActionMode.Callback 这样的接口。第二步调用startActionMode() 方法。

第一步:

private ActionMode mActionMode;private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {// Called each time the action mode is shown. Always called after// onCreateActionMode, but// may be called multiple times if the mode is invalidated.@Overridepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {return false; // Return false if nothing is done}// Called when the action mode is created; startActionMode() was called@Overridepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {// Inflate a menu resource providing context menu itemsMenuInflater inflater = mode.getMenuInflater();inflater.inflate(R.menu.game_menu, menu);return true;}@Overridepublic void onDestroyActionMode(ActionMode mode) { mActionMode = null;}@Overridepublic boolean onActionItemClicked(ActionMode mode, MenuItem item) {int id = item.getItemId();return false;}};

第二步:

mActionMode = startActionMode(mActionModeCallback);mActionMode.setTitle("title");

指定样式:

  <style name="MyActionBarTheme" parent="@style/Theme.AppCompat.Light">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:actionModeBackground">@android:color/holo_orange_light</item>        <!-- ActionMode右边的按钮是一个特殊的CloseButton,分割线与CloseButton的Style有关 -->        <!-- 删除ActionMode的Divider-->        <item name="android:actionModeCloseButtonStyle">@null</item>    </style>    <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">        <item name="android:background">@android:color/holo_green_light</item>    </style>













0 0