Android 无论上下文模式是否启用,动作栏项目的样式都会有所不同?
我的用例是这样的:默认的操作栏显示蓝色背景,我希望按钮在按下时变为绿色;另一方面,上下文操作栏是绿色的,我希望按钮在按下时变成蓝色。(有点反颜色)Android 无论上下文模式是否启用,动作栏项目的样式都会有所不同?,android,android-actionbar,android-theme,android-styles,android-contextmenu,Android,Android Actionbar,Android Theme,Android Styles,Android Contextmenu,我的用例是这样的:默认的操作栏显示蓝色背景,我希望按钮在按下时变为绿色;另一方面,上下文操作栏是绿色的,我希望按钮在按下时变成蓝色。(有点反颜色) 默认操作栏:蓝色背景,绿色覆盖(按下状态) 上下文操作模式:绿色背景,蓝色叠加(按下 (州) 我已经有了选择器,我可以在我的主题中设置android:actionBarItemBackground,为两种模式设置可绘制。我还可以在android中设置关闭按钮的样式:actionModeCloseButtonStyle,它工作正常 那么我如何设计其
- 默认操作栏:蓝色背景,绿色覆盖(按下状态)
- 上下文操作模式:绿色背景,蓝色叠加(按下 (州)
Gil正如我在评论中所说,
菜单项的视图无法访问,因此您没有任何直接的选项来访问它们。为那些MenuItems
选择选择器的一种方法是使用MenuItems
,将动作视图设置为imageview
,以保持正常图标并更改那些imageview
的选择器。下面是一个例子:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menuFirst"
android:showAsAction="always"
android:actionLayout="@layout/image_menu_layout"/>
</menu>
<!-- image_menu_layout.xml -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="@style/Widget.ActionButton"/>
ActionMode
部分的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.your_menu, menu);
// call this for any menu item that you might have
setUpMenuItem(menu, R.id.menuFirst, false, null);
return super.onCreateOptionsMenu(menu);
}
/**
* This methods set up the MenuItems.
*
* @param menu the menu reference, this would refer either to the menu of the ActionBar or
* the menu of the ActionMode
* @param itemId the id of the MenuItem which needs work
* @param onActionMode flag to indicate if we're working on the ActionBar or ActionMode
* @param modeRef the ActionMode reference(only when the MenuItem belongs to the ActionMode,
* null otherwise)
*/
private void setUpMenuItem(Menu menu, int itemId, final boolean onActionMode,
final ActionMode modeRef) {
final MenuItem menuItem = menu.findItem(itemId);
ImageView itemLayout = (ImageView) menuItem.getActionView();
itemLayout.setBackgroundResource(onActionMode ? R.drawable.selector_for_actionmode : R
.drawable.selector_for_actionbar);
itemLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// for simplicity, wire up the normal selection callbacks(if possible,
// meaning the Activity implements the ActionMode.Callback)
if (onActionMode) {
onActionItemClicked(modeRef, menuItem);
} else {
onOptionsItemSelected(menuItem);
}
}
});
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.actiomode_menu, menu);
// call this for any menu item that you might have
setUpMenuItem(menu, R.id.menu_item_from_actionmode, true, mode);
return true;
}
此方法还允许您在使用活动
时避免处理/保持操作栏
/操作模式
的状态。您无法真正访问这些菜单项来动态更改选择器,我记不起您可以使用的另一个主题属性。解决问题的一种方法是将ActionBar
和ActionMode
项目的标准菜单项替换为带有操作视图的菜单项(如简单的ImageView
),并在ImageView
上设置适当的(不同的)选择器(您还需要处理与ImageView
a.k.a新MenuItem
的交互)。听起来不错,我没有想到用自定义操作视图替换按钮。我将对其进行编码以评估维护成本,但看起来没问题。让我们看看是否有更直接的解决方案,否则您可以将其写在回答中,以便我可以接受。感谢您提供的代码。您可能需要添加样式=“@style/Widget.AppCompat.ActionButton”
添加到ImageView(或API 11+上的等效样式),使ImageView看起来像操作项按钮。@吉尔我已经编辑了我的答案,并将样式添加到ImageView
。谢谢,但您想使用style=“@style/Widget.ActionButton”“
相反,因为您的实现只在API 11+上工作(menuItem.getActionView()
是API 11+,兼容版本是MenuItemCompat.getActionView(menuItem)
)。在我之前的评论中,我粘贴了使用appcompat的我自己的实现中的行