本文介绍了Android Selector Drawable with VectorDrawables srcCompat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与 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 包含几个 VectorDrawable,所以我想我应该使用 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.

将此 SelectorDrawable 与 srcCompat 一起使用时,出现此错误:

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
  • 支持库 23.3.0
  • vectorDrawables.useSupportLibrary = true
  • Gradle 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 的每个活动:

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.

这篇关于Android Selector Drawable with VectorDrawables srcCompat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 03:21