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

5

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

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

我没有成功地动态更改颜色,就像许多人在stackoverflow上一样。

4个回答

5

这主要取决于您想自定义的项目。

基本上,您可以创建一个自定义颜色,其颜色会根据其状态而改变:

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"

1
我无法完成这个任务,因为getActionView()返回null。 - user3199693
@user3199693,请发布您的menu.xml文件代码。更具体地说,我们需要您正在尝试自定义的菜单项代码。 - Simas
我只有一个菜单项,包含id和文本。没有什么特别的。textColor属性不可访问。 - user3199693
@user3199693 你可以创建自己的自定义布局,并使用 setActionView() 应用到菜单项上。然后,你可以通过编程或者使用你所使用的布局的 xml 来设置它。 - Simas

1

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

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>

0

对于其他人可能需要:使用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>

0

使用:

<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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接