禁用“材质”按钮的颜色状态

问题描述 投票:0回答:2

Material Spec显示一个看起来为灰色的禁用按钮状态。

https://www.material.io/design/components/buttons.html#toggle-button

我正在使用来自Android的材料组件的MaterialButton:https://www.material.io/develop/android/components/material-button/

但是,当将按钮设置为禁用时,按钮的颜色/色调不会改变。

<com.google.android.material.button.MaterialButton
    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"/>

默认情况下,没有在Material Android Components中实现? Material Components是否定义了禁用的按钮状态列表?

android android-layout colors material-design android-button
2个回答
2
投票
  1. 创建文件夹/res/color(在res目录中)。
  2. 在这里添加一个新的颜色资源文件,名称类似于color_states_materialbutton.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="@color/colorDisabled"  />
    <item android:color="@color/colorEnabled" />
</selector>
  1. 在styles.xml中创建一个样式,其中一个Widget.MaterialComponents.Button样式作为其父项,颜色状态列表作为backgrountTint标记:
<style name="MaterialButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
        <item name="backgroundTint">@color/color_states_materialbutton</item>
</style>
  1. 在布局中的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"/>

0
投票

你应该使用ThemeOverlay并分别应用彩色样式

    <style name="AccentButton" parent="ThemeOverlay.AppCompat.Dark">
         <!-- customize colorButtonNormal for the disable color -->
         <!-- customize colorAccent for the enabled color -->
    </style>

    <Button
        android:id="@+id/login_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/fragment_login_login_button"
        android:theme="@style/AccentButton"
        style="@style/Widget.AppCompat.Button.Colored"/>
© www.soinside.com 2019 - 2024. All rights reserved.