【问题标题】:change drawable of selected MaterialButton using MaterialButtonToggleGroup使用 MaterialButtonToggleGroup 更改所选 MaterialButton 的可绘制对象
【发布时间】:2021-06-12 12:31:07
【问题描述】:

我无法更改所选 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>

样式 ToggleButton.Rounded:

 <style name="ToggleButton.Rounded" parent="ShapeAppearance.MaterialComponents.SmallComponent">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
</style>

代码的结果是:

我想要达到的目标:

【问题讨论】:

  • 你能分享一下 ToggleButton.Rounded 样式里面的代码吗?

标签: android android-button material-components-android android-togglebutton


【解决方案1】:

你必须使自定义drawable

制作一个可绘制的资源文件名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>

然后制作 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>

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>

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

【讨论】:

  • 问题是你不应该像文档中提到的那样在材质按钮上使用背景属性。
  • 您可以将 drawable 设置为背景,但是当您使用颜色作为背景时,它将无法正常工作此代码对我来说工作正常
  • 将背景设置为 MaterialButton 会引发运行时异常
猜你喜欢
  • 2020-02-01
  • 1970-01-01
  • 2019-09-29
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多