How to use svgs in selector for pre lollipop version?

亡梦爱人 提交于 2020-01-07 02:29:10

问题


I used selector for checkbox it works, but it causes problems for pre lollipop version

<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"/>

it causes resourcNotFoundexception, How to solve the problem, is there any solution?

FATAL EXCEPTION: main 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

回答1:


Try this.

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



回答2:


1) In your build.gradle:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true 
    }
}

2) In your Application class:

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

3) Use app:buttonCompat insted of android:button for CheckBox or buttonCompat insted of android:button your in style.xml

4) In my case I needed to add

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

parameters in my CheckBox for onClick reaction.

5) And in my case I needed to use androidx.appcompat.widget.AppCompatCheckBox instead of CheckBox in xml layout.

The article was helpfull.



来源:https://stackoverflow.com/questions/38083846/how-to-use-svgs-in-selector-for-pre-lollipop-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!