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是否定义了禁用的按钮状态列表?
/res/color
(在res
目录中)。<?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>
Widget.MaterialComponents.Button
样式作为其父项,颜色状态列表作为backgrountTint
标记:<style name="MaterialButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="backgroundTint">@color/color_states_materialbutton</item>
</style>
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"/>
你应该使用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"/>