AppCompatRadioButton 使用矢量资源

AppCompatRadioButton using vector asset

我在 4.4 设备上使用 Vector Drawables 时遇到问题。首先,我将 RadioButton 切换为 AppCompatRadioButton,这样我就可以使用向量了。事情是我不确定我应该如何使用它们,因为我在 xml (indicator_selector) 中有指示器,它基本上只是点击状态的打开和关闭资产:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/on" android:state_checked="true"/>
    <item android:drawable="@drawable/of" android:state_checked="false"/>

</selector>

因为这是矢量绘图,我应该将它们更改为

app:srcCompat=@drawable/on?

在代码中创建按钮时,我应该使用任何其他特定方式来绘制矢量可绘制对象吗?这是我当前的代码:

AppCompatRadioButton radioBtn = new AppCompatRadioButton(activity);
radioBtn.setButtonDrawable(R.drawable.indicator_selector);

你有吗

AppCompatDelegate.setCompatVectorFromResourcesEnabled( true ); // enable SVG

在您的应用中 class ?

所以基本上我无法让它与 XML 文件一起工作,我只是为

这样的状态编写了代码
StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_checked}, checked);
        stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, unchecked);

然后将其设置为 AppCompatRadioButton 即可运行:)