You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改MaterialButtonToggleGroup选中按钮的背景色(保留原透明度)

问题:修改MaterialButtonToggleGroup选中按钮的背景色并保留透明度

我的MaterialButtonToggleGroup XML代码如下:

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/type_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/welcome_text"
    app:selectionRequired="true"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/total_silence_button"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/total_silence"
        android:textColor="@color/light_green_700"
        android:textStyle="bold"
        app:rippleColor="@color/light_green_700"
        app:strokeColor="@color/light_green_700" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/priority_only_button"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/priority_only"
        android:textColor="@color/light_green_700"
        android:textStyle="bold"
        app:rippleColor="@color/light_green_700"
        app:strokeColor="@color/light_green_700" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/calls_only_button"
        style="?attr/materialButtonOutlinedStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundTint="@color/light_green_700"
        android:text="@string/calls_only"
        android:textStyle="bold"
        app:iconTint="@color/light_green_700"
        app:rippleColor="@color/light_green_700" />

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

未选中时效果:
未选中效果

选中按钮后,默认背景色为偏紫色:
选中效果

我想修改这个偏紫色背景色,同时保留原有透明度,但找不到对应的修改属性。


解决方案

方法1:自定义颜色状态列表(最简单)

Material按钮的选中背景色由backgroundTint控制,我们可以通过颜色状态列表指定选中时的颜色和默认透明度:

  1. 在res/color文件夹下新建button_toggle_selected_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 选中状态:使用目标颜色+默认透明度0.12 -->
    <item android:color="@color/light_green_700" android:alpha="0.12" android:state_checked="true"/>
    <!-- 未选中状态:透明 -->
    <item android:color="@android:color/transparent"/>
</selector>
  1. 给每个MaterialButton添加属性app:backgroundTint="@color/button_toggle_selected_bg",或者直接在按钮样式里统一配置,避免重复代码。

方法2:统一自定义按钮样式

如果要给所有ToggleGroup按钮统一设置样式,可在res/styles.xml中定义自定义样式:

<style name="CustomOutlinedToggleButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="backgroundTint">@color/button_toggle_selected_bg</item>
    <item name="strokeColor">@color/light_green_700</item>
    <item name="textColor">@color/light_green_700</item>
    <item name="rippleColor">@color/light_green_700</item>
</style>

然后将按钮的style属性替换为这个自定义样式:

style="@style/CustomOutlinedToggleButton"

方法3:代码动态设置

如果需要在代码中动态调整,可通过ColorStateList实现:

val targetColor = ContextCompat.getColor(this, R.color.light_green_700)
// 0x1F对应12%左右的透明度,和默认效果一致
val selectedColorWithAlpha = targetColor and 0xFFFFFF or (0x1F shl 24)
val colorStateList = ColorStateList(
    arrayOf(
        intArrayOf(android.R.attr.state_checked),
        intArrayOf()
    ),
    intArrayOf(selectedColorWithAlpha, Color.TRANSPARENT)
)
// 给所有按钮设置该状态列表
total_silence_button.backgroundTintList = colorStateList
priority_only_button.backgroundTintList = colorStateList
calls_only_button.backgroundTintList = colorStateList

内容的提问来源于stack exchange,提问作者Goutham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 06:30:37