【问题标题】:How to change material button disabled state background color in my theme?如何在我的主题中更改材质按钮禁用状态背景颜色?
【发布时间】:2020-02-24 08:57:29
【问题描述】:

我在我的 android 项目中使用 Material Design 组件。

我的问题是我无法更改材质按钮 (https://material.io/develop/android/components/material-button/) 的 DISABLED 背景颜色。

我想在我的主题中更改 DISABLED 颜色,但我不知道该怎么做。

我尝试查看Widget.MaterialComponents.Button 样式,发现它具有"backgroundTint" 属性:

<item name="backgroundTint">@color/mtrl_btn_bg_color_selector</item>

但它没有禁用状态样式,见下文:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>

我总是可以自己添加它,但是 DISABLED 按钮的初始灰显颜色是从哪里来的?

在我的主题中全局更改此颜色的最佳方法是什么?

附:我正在使用Theme.MaterialComponents.NoActionBar 主题。

谢谢!

【问题讨论】:

    标签: android material-design android-button material-components material-components-android


    【解决方案1】:

    禁用状态使用的颜色是选择器的最后一行:

    <item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
    

    全局更改,只需在您的应用主题中使用 materialButtonStyle 属性。

    <style name="AppTheme" parent="Theme.MaterialComponents.*">
      <item name="materialButtonStyle">@style/My.Button</item>
    </style>
    

    然后您可以根据自己的喜好自定义样式。

    <style name="My.Button" parent="@style/Widget.MaterialComponents.Button">
     <item name="backgroundTint">@color/my_color_selector</item>
     ...
    </style>
    

    或新的materialThemeOverlay 属性来覆盖颜色(它需要1.1.0 版本的库):

    <style name="My.Button" parent="@style/Widget.MaterialComponents.Button">
     <item name="materialThemeOverlay">@style/MyButtonThemeOverlay</item>
     ...
    </style>
    
    <style name="MyButtonThemeOverlay">
       <item name="colorOnSurface">@color/mycolor</item>
    </style>
    

    然后在您的布局中添加 Button without 样式。它将使用由My.Button 样式全局定义的样式。

    <com.google.android.material.button.MaterialButton
      .../>
    

    【讨论】:

    • 这有效,而可绘制选择器不起作用!但是为什么呢??
    • 由于某种原因这对我不起作用(对于 colorOnSurface/disabled 按钮颜色覆盖):(
    • 哦啦啦,这很好。我看不到如何让按钮使用 colorSecondary,并保留它的禁用颜色,而无需将 backgroundTint 指定为选择器。我认为最好的解决方案就是使用主题覆盖
    【解决方案2】:

    1.创建文件夹 res/color。 2.创建像color.xml这样的xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false"
            android:color="@color/colorDisabled"  />
        <item android:color="@color/colorEnabled" />
    </selector>
    

    3.在 styles.xml 中使用父 Widget.MaterialComponents.Button 创建样式。

    <style name="MaterialButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
            <item name="backgroundTint">@color/color_states_materialbutton</item>
    </style>
    

    4.在布局中设置 MaterialButton 的样式:

    <com.google.android.material.button.MaterialButton
        style="@style/MaterialButtonStyle"
        android:id="@+id/disabled_material_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="@string/button_label_disabled"/>
    

    【讨论】:

    • 这对我有用。以前从未在 res 文件夹中使用过颜色文件夹。很酷。
    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    相关资源
    最近更新 更多