【发布时间】:2014-10-18 17:40:09
【问题描述】:
是否可以根据其启用/禁用状态为菜单项定义不同的样式?
例如,我希望菜单项的文本颜色在禁用模式下为灰色,在启用模式下为白色。
我没有成功地改变颜色,就像很多人没有在 stackoverflow 上一样。
【问题讨论】:
标签: android android-actionbar menuitem
是否可以根据其启用/禁用状态为菜单项定义不同的样式?
例如,我希望菜单项的文本颜色在禁用模式下为灰色,在启用模式下为白色。
我没有成功地改变颜色,就像很多人没有在 stackoverflow 上一样。
【问题讨论】:
标签: android android-actionbar menuitem
这实际上取决于您要自定义的项目。
基本上,您可以创建一个根据其状态变化的自定义颜色:
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);
或者如果可用,也可以在 xml 中:
android:textColor="@color/custom_color"
【讨论】:
setActionView() 的菜单项。然后,您可以通过编程方式或通过您使用的布局的 xml 进行设置。
可能需要其他一些:使用 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>
【讨论】:
您可以使用drawable作为文本颜色,在drawable中您可以使用选择器根据启用状态选择颜色。使用以下可绘制定义作为颜色将使您禁用的菜单项变为灰色,其余的为黑色。
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>
然后,使用drawable:
<item name="android:textColor">@drawable/default_text_colour</item>
【讨论】:
使用:
<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
【讨论】: