首页 > 代码库 > contextual action mode
contextual action mode
在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即是右边的那幅图。
我实现的效果如下图:
- 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. - Call
startActionMode()
when you want to show the bar (such as when the user long-clicks the view).
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. @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; // Return false if nothing is done } // 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.game_menu, menu); return true; } @Override public void onDestroyActionMode(ActionMode mode) { mActionMode = null; } @Override public 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>
contextual action mode
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。