是否可以根据菜单项的启用/禁用状态定义不同的样式?
例如,我希望在禁用模式下,菜单项的文本颜色为灰色,在启用模式下为白色。
我没有成功地动态更改颜色,就像许多人在stackoverflow上一样。
是否可以根据菜单项的启用/禁用状态定义不同的样式?
例如,我希望在禁用模式下,菜单项的文本颜色为灰色,在启用模式下为白色。
我没有成功地动态更改颜色,就像许多人在stackoverflow上一样。
这主要取决于您想自定义的项目。
基本上,您可以创建一个自定义颜色,其颜色会根据其状态而改变:
colors/custom_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
menu.findItem(R.id.action_search).getActionView().
setBackgroundResource(R.colors/custom_color.xml);
android:textColor="@color/custom_color"
您可以使用可绘制对象作为文本颜色,在可绘制对象中,您可以使用选择器来根据启用状态选择颜色。使用以下可绘制对象定义作为颜色将使您的禁用菜单项变灰,其余部分为黑色。
In e.g. res/drawable/default_text_colour.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@android:color/darker_gray"/>
<item android:color="@android:color/white"/>
</selector>
<item name="android:textColor">@drawable/default_text_colour</item>
对于其他人可能需要:使用ToolBar而不是ActionBar,添加TextView并将其样式设置为MenuItem:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_explorer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/CustomActionBar.Theme"
android:background="@mipmap/bg_naviber">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/title_activity_explorer2"
android:layout_gravity="start"
android:padding="@dimen/dimen_8"
style="@style/CustomActionBar.Tittle"
/>
<TextView
android:id="@+id/toolbar_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="@string/action_delete"
android:textColor="@drawable/selector_text_view"
android:padding="@dimen/dimen_16"
style="@style/CustomActionBar.Menu"
/>
<TextView
android:id="@+id/toolbar_do_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="@string/action_do_delete"
android:textColor="@drawable/selector_text_view"
android:padding="@dimen/dimen_16"
style="@style/CustomActionBar.Menu"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>
通过选择器设置文本颜色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- order is important -->
<item android:state_enabled="false" android:color="@color/white_disabled"/>
<item android:state_pressed="true" android:color="@color/white"/>
<item android:color="@color/white"/>
</selector>
使用:
<style name="Theme.WordsTrainer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:textColor">?android:attr/textColorPrimary</item>
关于 themes.xml 和 night\themes.xml 的问题,我已经解决了。现在无论是白天模式还是夜间模式都可以正常工作。
我花了一整天的时间进行了挖掘,并最终在显然的地方找到了答案: https://developer.android.com/guide/topics/ui/look-and-feel/darktheme
setActionView()
应用到菜单项上。然后,你可以通过编程或者使用你所使用的布局的 xml 来设置它。 - Simas