标签:
在Google的开发文档的guide的menu里面,提到上下文菜单的两种形式。
There are two ways to provide contextual actions:
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即是右边的那幅图。
我实现的效果如下图:
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.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>
标签:
原文地址:http://blog.csdn.net/chenfuduo_loveit/article/details/43267415