我在新的向后兼容性方面遇到了问题,与VectorDrawables相关。 在Support Library 23.2中,引入了一项用于兼容Android VectorDrawables的新功能。
我有一个ImageView,它被赋予了SelectorDrawable。这个Drawable包含了几个VectorDrawables,所以我认为应该使用app:srcCompat进行兼容性处理。但是在我的Galaxy S2上,Android 4.1.2上不能使用。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_gps_fixed_24dp"android:state_activated="true" android:state_selected="true"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp" android:state_activated="true" android:state_selected="false"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp" android:state_activated="false" android:state_selected="true"></item>
<item android:drawable="@drawable/ic_gps_off_24dp" android:state_activated="false" android:state_selected="false"></item>
<item android:drawable="@drawable/ic_gps_not_fixed_24dp"></item>
</selector>
所有的可绘制对象都是矢量 XML 文件。
当我在使用这个 SelectorDrawable 与 srcCompat 一起时,我遇到了这个错误:
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ Caused by: android.content.res.Resources$NotFoundException: File res/drawable/ic_gps_fixed_24dp.xml from drawable resource ID #0x7f0201c1
at android.content.res.Resources.loadDrawable(Resources.java:1951)
at android.content.res.Resources.getDrawable(Resources.java:672)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:173)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:881).xml from drawable resource ID #0x7f0201c1
使用android:src甚至更糟。
如果我使用其中一个矢量可绘制对象与app:srcCompat一起使用,则所有工作都正常。因此,我想这是SelectorDrawable和兼容性的问题。
是否有人遇到过同样的问题并找到了解决方法,或者目前在Android 5之前无法在SelectorDrawables中使用VectorDrawables?
快速事实:
- 编译目标API 23
- 支持库23.3.0
- vectorDrawables.useSupportLibrary = true
- Gradle 2.0