【问题标题】:Change the background color of MaterialButtonToggleGroup更改 MaterialButtonToggleGroup 的背景颜色
【发布时间】:2021-05-05 10:16:01
【问题描述】:

我正在使用 MaterialButtonToggleGroup 创建选择器按钮。 我想更改 MaterialButton 按钮的背景颜色。它的默认颜色是浅蓝色,我想把它改成浅绿色。目前,我正在使用可绘制扇区来更改背景颜色,但它不起作用。

默认颜色

想要这个颜色,

这是我的布局,

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/toggleContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:checkedButton="@id/btnOutline"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    android:visibility="visible"
    app:layout_constraintRight_toRightOf="parent"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnOutline"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_selector"
        android:text="@string/outline"
        android:textAllCaps="false" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnMain"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:background="@drawable/button_selector"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main"
        android:textAllCaps="false" />

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

这是我的可绘制文件“button_selector”,

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@drawable/selector_color"/>

</selector>

这里是“selector_color”文件,

  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#93cdcb"/>
</shape>

【问题讨论】:

    标签: android material-design android-togglebutton materialbutton


    【解决方案1】:

    对于材质按钮,您应该使用 app:backgroundTint 而不是 android:background Android 文档在这里也提到了它: https://developer.android.com/reference/com/google/android/material/button/MaterialButton

    不要使用 android:background 属性。 MaterialButton 管理 它自己的背景可绘制,并设置一个新的背景意味着 MaterialButton 不能再保证它的新属性 引入将正常运行

    对于填充按钮,此类使用您的主题的 ?attr/colorPrimary 用于背景色调颜色和 ?attr/colorOnPrimary 用于文本 颜色。对于未填充的按钮,此类使用 ?attr/colorPrimary 背景色调的文本颜色和透明度。

    【讨论】:

      【解决方案2】:

      您可以尝试在“android:backgroundTint”属性上设置选择器,而不是将其设置为“android:background”。如果您想更新边框颜色,请考虑更新和 'app:strokeColor'

      【讨论】:

        【解决方案3】:

        MaterialButton 的背景颜色由 backgroundTint 属性定义。

        默认值为:

        <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>
        

        您可以在xml布局中更改它,也可以定义自定义样式:

        <com.google.android.material.button.MaterialButton
                   style="@style/ButtonStyle"
                   .. />
        

        与:

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

        【讨论】:

        【解决方案4】:

        我遇到了同样的问题,并通过在选择器中使用 android:state_checked="true" 而不是 android:state_selected="true" 解决了它。

        【讨论】:

          猜你喜欢
          • 2013-08-08
          • 1970-01-01
          • 2012-04-21
          • 2017-08-11
          • 2015-10-24
          • 2015-06-23
          相关资源
          最近更新 更多