【问题标题】:How to use svgs in selector for pre lollipop version?如何在棒棒糖前版本的选择器中使用 svgs?
【发布时间】:2016-06-28 18:38:11
【问题描述】:

我将选择器用于复选框,它可以工作,但它会导致棒棒糖前版本出现问题

<selector xmlns:android="...">
    <item android:state_checked="true"   
        android:drawable="@drawable/checked_icon" />
    <item android:drawable="@drawable/icon" />
</selector>




<CheckBox
 android:button="@drawable/terms_checkbox"/>

导致resourceNotFoundexception,如何解决,有什么解决办法吗?

致命异常:主要 android.view.InflateException: Binary XML file line #70: Error inflating class CheckBox

     at dalvik.system.NativeStart.main(Native Method)
                                                                   Caused by: android.content.res.Resources$NotFoundException: File res/drawable/terms_service1.xml from drawable resource ID #0x7f020121
                                                                      at android.content.res.Resources.loadDrawable(Resources.java:2096)
                                                                      at android.content.res.TypedArray.getDrawable(TypedArray.java:601)

                                                                      at android.widget.CompoundButton.<init>(CompoundButton.java:74)
                                                                      at android.widget.CheckBox.<init>(CheckBox.java:68)


Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #1: invalid drawable tag vector
                                                                    at android.graphics.drawable.Drawable.createFromXmlInner(Draw

【问题讨论】:

  • 适用于 Android 4.3 版。
  • 我正在模拟器 android 版本 4.3.1 android.content.res.Resources$NotFoundException 上进行测试:
  • gradle中使用的buildToolsVersion是什么??
  • buildToolsVersion "23.0.3" 编译 'com.android.support:appcompat-v7:23.4.0'
  • 同时设置vectorDrawables.useSupportLibrary = true

标签: android svg android-vectordrawable


【解决方案1】:

试试这个。

    CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
    Drawable d = AppCompatDrawableManager.get().getDrawable(this, R.drawable.terms_checkbox);
    checkBox.setButtonDrawable(d);

【讨论】:

    【解决方案2】:

    1) 在你的build.gradle:

    android {
        defaultConfig {
            vectorDrawables.useSupportLibrary = true 
        }
    }
    

    2) 在您的应用程序类中:

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
    

    3) 使用 app:buttonCompat insted of android:button 作为 CheckBox 或 buttonCompat 的 android:button 你在 style.xml 中

    4) 就我而言,我需要添加

    android:focusable="true"
    android:clickable="true"
    

    我的 CheckBox 中用于 onClick 反应的参数。

    5) 在我的情况下,我需要使用 androidx.appcompat.widget.AppCompatCheckBox 而不是 xml 布局中的 CheckBox

    article 很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多