问题描述
我正面临着与VectorDrawables的新向后兼容性的问题.支持库23.2中提供了一项新功能,用于与向后兼容的Android VectorDrawables引入.
I'm facing a problem with the new backward compatibility with VectorDrawables.In the Support Library 23.2 was a new feature for backward compatibility with Android VectorDrawables indroduced.
我有一个ImageView,它是分配给它的SelectorDrawable.这个Drawable拥有几个VectorDrawables,所以我认为我应该使用app:srcCompat来实现兼容性.但这在使用Android 4.1.2的Galaxy S2上不起作用.
I have an ImageView which is a SelectorDrawable assigned to. This Drawable holds several VectorDrawables so I thought I should use app:srcCompat for compatibility. But it does not work on my Galaxy S2 with 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文件.
All drawables are vector xml files.
在与srcCompat一起使用此SelectorDrawable时,出现此错误:
When using this SelectorDrawable with srcCompat I get this error:
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更加糟糕.
using android:src is even worse.
如果我将矢量可绘制对象之一与app:srcCompat一起使用,则一切正常.所以我想这与SelectorDrawable和兼容性有关.
If I use one of the vector drawables with app:srcCompat all works fine. So I guess it's a problem with the SelectorDrawable and compatibility.
有人遇到过同样的问题并找到了解决方案吗?或者当前无法在Android 5之前的SelectorDrawables中使用VectorDrawables吗?
Has anyone had the same problem and found a solution or is it currently not possible to use VectorDrawables in SelectorDrawables prior to Android 5?
事实速览:
- 编译目标API 23
- 支持Libraray 23.3.0
- vectorDrawables.useSupportLibrary = true
- 2.0版
推荐答案
自从我问了这个问题以来,有些事情已经改变,所以我自己回答.
Some things have changed since I asked this question, so I will answer it myself.
使用支持库23.4.0重新启用了对Ressources中VectorDrawables的支持: Android支持库23.4 .0现在可用
With Support Library 23.4.0 the support for VectorDrawables from Ressources was reenabled: Android Support Library 23.4.0 available now
您可以在Google I/O 2016的此演员表中找到有关此内容的更多信息:支持库中的新功能-Google I/O 2016
You can find more information on that in this cast from the Google I/O 2016:What's new in the support library - Google I/O 2016
您需要将此代码添加到要在Android 5.0以下设备(代号Lollipop,API级别21)上使用VectorDrawables的每个Activity中:
You need to add this to every Activity where you want to use VectorDrawables on devices below Android 5.0 (Codename Lollipop, API level 21):
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
因此,您现在可以在DrawableContainers中使用VectorDrawables,但它仍可能会导致上述来源中提到的某些问题,因此请谨慎使用.
So you can now use VectorDrawables in DrawableContainers but it can still cause some issues as mentioned in the sources above so use it with caution.
到目前为止,我尚未在应用程序中重新启用此功能,但在下一个主要版本中,我会将很多图标更改为VectorDrawables,然后将深入探讨该主题.
I did not reenable this feature in my app so far but I will change a lot of my icons to VectorDrawables with my next major release and will then dive deeper into this topic.
这篇关于带有VectorDrawables srcCompat的Android Selector Drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!