Using SVG in android selector for buttons

怎甘沉沦 提交于 2021-02-08 15:16:24

问题


I am using svg for icons for ImageView it can be given using app:srcCompat But when i want to use it for Buttons as selector the app crashes with resource not found exception for devices with api below 21

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_enabled" android:state_enabled="true" />
    <item android:drawable="@drawable/button_disabled" android:state_enabled="false" />
</selector>

Where button_enabled and button_disabled are both svg


回答1:


I have the answer to my own question. In your activity just add this static block

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}



回答2:


I suggest you to follow the below steps :-

Step 1:

Right click on drawable folder -> select New -> select Vaector Assests

Step 2:

Select Local file svg now select the path of your svg image click on next you will have now the svg image in your drawable folder

do the same thing for svg images you want to use in your application

Now use the below code for selector buttons selector_button.xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_check_circle" android:state_pressed="true" />
    <item android:drawable="@drawable/ic_build" android:state_focused="true" />
    <item android:drawable="@drawable/ic_build" />
</selector>

I imported two svg 1st ic_check_circle and 2nd ic_build you can replace as per your needs.

In your imageView use below line

app:srcCompat="@drawable/selector_button" 



回答3:


SVG rendering in Android was targeted for API level 21 and +. ie from Lollipop onward. Since the crash occurs in a lower version, what you require is backward compatibility for SVG .

Ideally there are two solutions here:

  1. Use the Victor library, which supports backward compatibility. Available [ here ] . Also check this detailed medium article on Vector usage in Android. [ here ]
  2. From Android support library v23.2 onward, SVG usage has been made backward compatible till API v 7. The detailed guide is available [ here ]. Also see this Answer as well. [ here ]



回答4:


Use below selector_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_check_circle" android:state_checked="true" android:state_focused="true" />
    <item android:drawable="@drawable/ic_uncheck_circle" android:state_checked="false" android:state_focused="true" />
    <item android:drawable="@drawable/ic_uncheck_circle" android:state_checked="false" />
    <item android:drawable="@drawable/ic_check_circle" android:state_checked="true" />
</selector>

Where ic_check_circle and ic_uncheck_circle both are svg file imported from local file as i already mentioned in above steps

And in your xml file use below code for CheckBox and put selector_checkbox.xml file as a button and you are done

<android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/appCompatCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/selector_checkbox" />


来源:https://stackoverflow.com/questions/41802267/using-svg-in-android-selector-for-buttons

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