使用 MaterialButtonToggleGroup 更改所选 MaterialButton 的可绘制对象

Him*_*lik 5 android android-button android-togglebutton material-components-android

我无法更改所选 MaterialButton 的背景。

<com.google.android.material.button.MaterialButtonToggleGroup
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:singleSelection="true"
        app:selectionRequired="true"
        app:checkedButton="@+id/toggleButtonFolder">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/toggleButtonFolder"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:shapeAppearance="@style/ToggleButton.Rounded"

            android:text="Folders" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/toggleButtonRecent"
            style="?attr/materialButtonOutlinedStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:shapeAppearance="@style/ToggleButton.Rounded"

            android:text="Recent" />

    </com.google.android.material.button.MaterialButtonToggleGroup>
Run Code Online (Sandbox Code Playgroud)

样式 ToggleButton.Rounded:

 <style name="ToggleButton.Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

代码的结果是:

在此输入图像描述

我想达到的目标:

在此输入图像描述

小智 -1

你必须制作自定义可绘制的

制作一个可绘制资源文件,名称为 coustom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_window_focused="false"
    android:drawable="@drawable/btn_radio_on" />
  <item android:state_checked="false" android:state_window_focused="false"
    android:drawable="@drawable/btn_radio_off" />

  <item android:state_checked="false" android:drawable="@drawable/btn_radio_on" />
  <item android:state_checked="true" android:drawable="@drawable/btn_radio_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)

然后制作 btn_radio_on 和 btn_radio_off 可绘制文件

btn_radio_on.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
     <shape android:shape="rectangle">
         <stroke android:color="#3b91d7" android:width="1dp"/>
         <corners android:topLeftRadius="20dp"
             android:topRightRadius="20dp"
             android:bottomLeftRadius="20dp"
             android:bottomRightRadius="20dp"/>
     </shape>
 </item>
Run Code Online (Sandbox Code Playgroud)

btn_radio_off.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item>
     <shape android:shape="rectangle">
         <stroke android:color="#000000" android:width="1dp"/>
         <solid android:color="#82b1ff"/>

         <corners android:topLeftRadius="20dp"
             android:topRightRadius="20dp"
             android:bottomLeftRadius="20dp"
             android:bottomRightRadius="20dp"/>
     </shape>
 </item>
Run Code Online (Sandbox Code Playgroud)

现在将 coustom_btn_radio.xml 设置为 MaterialButton 的背景

  • 将背景设置为 MaterialButton 会引发运行时异常 (2认同)