【问题标题】:Android menu item define style for enabled/disabled statusAndroid 菜单项定义启用/禁用状态的样式
【发布时间】:2014-10-18 17:40:09
【问题描述】:

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

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

我没有成功地改变颜色,就像很多人没有在 stackoverflow 上一样。

【问题讨论】:

    标签: android android-actionbar menuitem


    【解决方案1】:

    这实际上取决于您要自定义的项目。

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

    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"
    

    【讨论】:

    • 我无法执行此操作,因为 getActionView() 返回 null
    • @user3199693 请发布您的 menu.xml 文件的代码。更具体地说,我们需要您尝试自定义的菜单项代码。
    • 我只有一个由 id 和 text 组成的菜单项。没什么特别的。 textColor 属性不可访问。
    • @user3199693 您可以创建自己的自定义布局并将其应用到带有setActionView() 的菜单项。然后,您可以通过编程方式或通过您使用的布局的 xml 进行设置。
    【解决方案2】:

    可能需要其他一些:使用 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>
    

    【讨论】:

      【解决方案3】:

      您可以使用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>
      

      【讨论】:

        【解决方案4】:

        使用:

        <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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多