web-dev-qa-db-ja.com

マテリアルボタンの無効なカラー状態

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コンポーネントはデフォルトで実装されていませんか?Material Componentsは無効なボタンのステートリストを定義していますか?

14
  1. resディレクトリに)フォルダー/res/colorを作成します。
  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. 親としてWidget.MaterialComponents.Buttonスタイルの1つを使用し、backgrountTintタグとしてカラー状態リストを使用して、スタイルをstyles.xmlで作成します。
<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"/>
19
tobiasfried

themeOverlayを使用して、Coloredスタイルを個別に適用する必要があります

    <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"/>
0
Vishrut Mavani