在 Radiobutton 上单击或关闭时样式没有变化

There is no change in style when click on or off on Radiobutton

我想在使用 @drawable/radio_selected 单击关闭或打开时制作样式 RadioButton,如下面的 2 个图像。

<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="@drawable/radio_selected"
        tools:ignore="UnusedAttribute" />

在这个 drawable 中,我使用选择器来设置单选按钮打开或关闭的条件。

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

rb_on.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="5dp"
    android:height="5dp"
    android:viewportWidth="19"
    android:viewportHeight="19">
  <path
      android:pathData="M9.5,9.5m-6.5,0a6.5,6.5 0,1 1,13 0a6.5,6.5 0,1 1,-13 0"
      android:strokeWidth="6"
      android:fillColor="#ffffff"
      android:strokeColor="#FF8830"/>
</vector>

rb_off.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="5dp"
    android:height="5dp"
    android:viewportWidth="19"
    android:viewportHeight="19">
  <path
      android:pathData="M9.5,9.5m-8.5,0a8.5,8.5 0,1 1,17 0a8.5,8.5 0,1 1,-17 0"
      android:strokeWidth="2"
      android:fillColor="#ffffff"
      android:strokeColor="#FF8830"/>
</vector>

但是,那是行不通的。结果是这样的

我添加了"android:state_pressed"等几项,但结果是一样的。 如何解决?

如果你想改变RadioButton的图标那么你需要使用android:button

使用这个

android:button="@drawable/radio_selected"

而不是这个

 android:background="@drawable/radio_selected"

示例代码

<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:button="@drawable/radio_selected"
        tools:ignore="UnusedAttribute" />

在 xml

中使用按钮而不是背景
android:background="@drawable/radio_selected" 

android:button="@drawable/radio_selected"