带矢量绘图的 RadioButton

RadioButton with vector drawables

我正在尝试使用 RadioButtons 制作性别切换器。现在看起来像 this.

我使用矢量绘图作为图标。我想让它 @color:colorAccent 并在按下按钮时将其背景更改为白色圆圈。怎么做比较好?

layout.xml

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginEnd="8dp"
                android:button="@drawable/custom_btn_radio"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton2"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:layout_weight="1"
                android:layout_marginStart="8dp"
                android:button="@drawable/custom_btn_radio"/>

        </LinearLayout>
    </RadioGroup>

custom_btn_radio.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
    android:drawable="@drawable/human_male"/>
<item android:state_checked="true"
    android:drawable="@drawable/human_male"
    android:background="@android:color/white"
    android:tint="@color/colorAccent"/>

您应该为不同的状态使用 <selector> 到 select 不同的可绘制对象。

然后对于颜色,当您使用矢量可绘制对象时,您可以为其属性设置动画:AnimVector因此您可以为颜色设置动画,即使它是一个持续时间为零的动画(理论上,这不是我做过的事)。