gpt4 book ai didi

android - 更改 MaterialButtonToggleGroup 的背景颜色

转载 作者:行者123 更新时间:2023-12-04 23:55:00 26 4
gpt4 key购买 nike

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

默认颜色enter image description here

想要这个颜色, enter image description here

这是我的布局,

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

最佳答案

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

Do not use the android:background attribute. MaterialButton managesits own background drawable, and setting a new background meansMaterialButton can no longer guarantee that the new attributes itintroduces will function properly

For filled buttons, this class uses your theme's ?attr/colorPrimaryfor the background tint color and ?attr/colorOnPrimary for the textcolor. For unfilled buttons, this class uses ?attr/colorPrimary forthe text color and transparent for the background tint.

关于android - 更改 MaterialButtonToggleGroup 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65989285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com