【问题标题】:AppCompatRadioButton using vector assetAppCompatRadioButton 使用矢量资源
【发布时间】:2019-01-30 16:37:18
【问题描述】:

我在 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);

【问题讨论】:

    标签: android android-vectordrawable android-radiobutton


    【解决方案1】:

    你有

    AppCompatDelegate.setCompatVectorFromResourcesEnabled( true ); // enable SVG

    在您的应用类中?

    【讨论】:

    • 我只包含在 gradle 中?
    • 您需要创建一个扩展 Application 的 App 类,然后在 onCreate 方法中放置此代码
    • 嗯好吧,但指标应该是什么?我把可绘制状态放在哪里? android:drawable 还是别的什么?
    • app:srcCompat=@drawable/indicator_selector 在你的 xml 文件中
    【解决方案2】:

    所以基本上我无法让它与 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 即可:)

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 2019-04-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多