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即是右边的那幅图。

我实现的效果如下图:



  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.
		@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>













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值