Android操作栏选项菜单项自定义可选背景

我正在尝试将默认的 android 操作栏与自定义视图一起使用:


<style name="ActionBar" parent="android:Widget.Material.ActionBar.Solid">

    <item name="android:displayOptions">showCustom</item>

    <item name="android:customNavigationLayout">@layout/customNavigationLayout</item>

</style>

选项菜单包含一个应始终显示文本的项目:


<item

    android:id="@+id/action_cancel"

    android:showAsAction="always|withText"

    android:title="@string/action_cancel" />

现在我遇到了可选背景的问题,它仍然是一个动作图标的大小:

http://img3.mukewang.com/6481c4ad0001888a03780078.jpg

如何设置操作栏以应用可选背景来填充整个项目框?

http://img.mukewang.com/6481c4ba000145cd03790079.jpg


慕妹3242003
浏览 364回答 3
3回答

守候你守候我

您可以尝试android:actionBarItemBackground在 中设置属性styles,如下所示:<style name="AppTheme" parent="android:Theme.Material">&nbsp; &nbsp; ...&nbsp; &nbsp; <item name="android:actionBarItemBackground">?android:selectableItemBackground</item></style>

慕婉清6462132

使用以下解决方案之一:解决方案 1:添加<item name="actionButtonStyle">@style/ActionButtonStyle</item>到您的基本应用程序主题,例如:<style name="ActionButtonStyle" parent="android:Widget.Material.Button">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item name="android:textColor">@android:color/black</item>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item name="android:textSize">16sp</item>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <item name="android:background">@drawable/button_background</item></style>和按钮背景<selector xmlns:android="http://schemas.android.com/apk/res/android">&nbsp; &nbsp; <item android:state_pressed="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawable="@color/colorAccent"/>&nbsp; &nbsp; <item android:state_focused="true"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:drawable="@color/colorPrimaryDark"/>&nbsp; &nbsp; <item android:drawable="@color/colorPrimary"/></selector>解决方案 2:用于menuItem.setActionView将自定义布局应用于您的菜单项,如下所示:layout_action_cancel(自定义菜单项布局):<Button&nbsp; &nbsp; &nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Cancel"&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/b_action_cancel"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:background="@drawable/button_background"></Button>然后在您的 onCreateOptionsMenu 中将自定义布局应用于菜单项:@Override&nbsp; &nbsp; public boolean onCreateOptionsMenu(Menu menu) {&nbsp; &nbsp; &nbsp; &nbsp; MenuInflater menuInflater = getMenuInflater();&nbsp; &nbsp; &nbsp; &nbsp; menuInflater.inflate(R.menu.menu, menu);&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater layoutInflater = getLayoutInflater();&nbsp; &nbsp; &nbsp; &nbsp; View layout = layoutInflater.inflate(R.layout.layout_action_cancel, null, false);&nbsp; &nbsp; &nbsp; &nbsp; menu.getItem(0).setActionView(layout);&nbsp; &nbsp; &nbsp; &nbsp; return super.onCreateOptionsMenu(menu);&nbsp; &nbsp; }并在 onPrepareOptionsMenu 中设置一个 OnClick 侦听器(替换 onOptionsItemSelected)@Override&nbsp; &nbsp; public boolean onPrepareOptionsMenu(Menu menu) {&nbsp; &nbsp; &nbsp; &nbsp; View layout = menu.getItem(0).getActionView();&nbsp; &nbsp; &nbsp; &nbsp; if(layout instanceof Button){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Button b_action_cancel = layout.findViewById(R.id.b_action_cancel);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b_action_cancel.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View view) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //your code&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return super.onPrepareOptionsMenu(menu);&nbsp; &nbsp; }

慕容708150

您可以尝试在菜单项属性中应用具有选定状态和正常状态的可绘制对象:<item&nbsp; &nbsp; android:id="@+id/action_cancel"&nbsp; &nbsp; android:showAsAction="always|withText"&nbsp; &nbsp; android:title="@string/action_cancel"&nbsp;&nbsp; &nbsp; android:icon="@drawable/mybuttonbackground" />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java