Android菜单项定义启用/禁用状态的样式

Android菜单项定义启用/禁用状态的样式,android,android-actionbar,menuitem,Android,Android Actionbar,Menuitem,是否可以根据菜单项的启用/禁用状态为其定义不同的样式 例如,我希望菜单项的文本颜色在禁用模式下为灰色,在启用模式下为白色 正如许多人不喜欢stackoverflow一样,我没有成功地日常更换颜色。这取决于您想要定制的物品 基本上,您可以创建根据其状态而变化的自定义颜色: colors/custom\u color.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <ite

是否可以根据菜单项的启用/禁用状态为其定义不同的样式

例如,我希望菜单项的文本颜色在禁用模式下为灰色,在启用模式下为白色


正如许多人不喜欢stackoverflow一样,我没有成功地日常更换颜色。

这取决于您想要定制的物品

基本上,您可以创建根据其状态而变化的自定义颜色:

colors/custom\u 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>
或者在xml中(如果可用):

android:textColor="@color/custom_color"

对于其他人,可能需要:使用工具栏而不是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>

您可以使用可绘制的文本颜色,在可绘制中,您可以使用选择器根据启用状态选择颜色。使用以下可绘制定义作为颜色将使禁用的菜单项变为灰色,其余为黑色

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>
在例如res/drawable/default\u text\u color.xml中:
然后,使用可绘图工具:

<item name="android:textColor">@drawable/default_text_colour</item>
@可绘制/默认\u文本\u颜色
使用:

<style name="Theme.WordsTrainer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

我添加了一行:

<item name="android:textColor">?android:attr/textColorPrimary</item>
android:attr/textColorPrimary 到themes.xml和night\themes.xml,这在日间和夜间模式下都可以正常工作

我花了一天时间研究它,终于在显而易见的地方找到了答案:

我无法执行此操作,因为getActionView()返回null@user3199693请发布menu.xml文件的代码。更具体地说,我们需要您试图自定义的菜单项代码。我只有一个由id和文本组成的菜单项。没什么特别的。textColor属性不可访问。@user3199693您可以创建自己的自定义布局,并使用
setActionView()
将其应用于菜单项。然后,您可以通过编程或使用布局的xml进行设置。
<item name="android:textColor">?android:attr/textColorPrimary</item>