支持库VectorDrawable Resources $ NotFoundException

问题描述 投票:62回答:13

我使用的是Design Support Library版本23.4.0。我启用了gradle标志:

defaultConfig {
    vectorDrawables.useSupportLibrary = true
}

我正在使用构建工具版本23.0.2,但仍然,我在KitKat或更低版本上获得Resources$NotFoundException

它发生在我使用android:drawableLeftimageView.setImageResource(R.drawable.drawable_image)时。

是的,我把它放在我使用drawables的每个活动上

static {
    AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}

这是支持库的错误吗?

android android-support-library androiddesignsupport android-vectordrawable
13个回答
72
投票

我需要使用支持库23.4.0才能使用3个单独的东西:

  1. 将其添加到build.gradle defaultConfig { vectorDrawables.useSupportLibrary = true }
  2. 将以下内容添加到Application类的onCreate中 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
  3. 对于要在其中设置矢量可绘制替换的所有xml视图 android:src app:srcCompat 并在代码中替换为: imageView.setImageResource(...); imageView.setImageDrawable(...);

1
投票

API 16 animation 膨胀Drawable的

此支持库中的`VectorDrawable``AnimatedVectorDrawable`可以通过以下方式膨胀:

  • 调用静态getDrawable()方法:
//This will only inflate a drawable with <vector> as the root element
VectorDrawable.getDrawable(context, R.drawable.ic_arrow_vector);

//This will only inflate a drawable with <animated-vector> as the root element
AnimatedVectorDrawable.getDrawable(context, R.drawable.ic_arrow_to_menu_animated_vector);

// This will inflate any drawable and will auto-fallback to the lollipop implementation on api 21+ devices
ResourcesCompat.getDrawable(context, R.drawable.any_drawable);

如果在java代码中膨胀Drawable,建议始终使用ResourcesCompat.getDrawable(),因为这适用于处理Lollipop后备。这允许系统缓存Drawable ConstantState,因此更有效。 该库具有以下变形(双向​​)动画:

  • 播放 - 暂停变形动画
  • 播放 - 停止变形动画
  • Arrow-Hamburger菜单变形动画

  • As you can see, I produced the above image on my API 16 phone:
    import com.wnafee.vector.compat.AnimatedVectorDrawable;
    mdrawable = (AnimatedVectorDrawable) AnimatedVectorDrawable.getDrawable(this.getApplicationContext(), R.drawable.consolidated_animated_vector);
    

    在这里查看vector-compat的github自述文件:https://github.com/wnafee/vector-compat 如果你将它与你的app模块的API 14 build.gradle(通常在文件的末尾)合并,这将解决你的问题(下至dependencies):

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    //Trying to FIX Binary XML file line #2: invalid drawable tag animated-vector
        compile 'com.android.support:appcompat-v7:25.0.0'
        compile 'com.android.support:design:25.0.0'
    //not needed
    //  compile 'com.android.support:support-vector-drawable:25.0.0'
        compile 'com.wnafee:vector-compat:1.0.5'//*******holy grail *******https://github.com/wnafee/vector-compat
    //  Failed to resolve: com.android.support:support-animated-vector-drawable:25.0.0
    //not needed
    //  compile 'com.android.support:support-animated-vector-drawable:25.0.0'
    }
    

    1
    投票

    不要把你的载体放在drawable-anydpi,旧设备不支持

    把它们放在drawable


    1
    投票

    在我的特定情况下,我遇到了这个问题,因为我使用了一个drawable选择器作为图像资源,在选择器中有几个向量,如:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:drawable="@drawable/vector_select_blue"/>
        <item android:state_pressed="true" android:drawable="@drawable/vector_select_black"/>
        .
        .
        etc
    </selector>
    

    是的,非常糟糕,但当时并不知道更好。

    因此,正确的方法是在矢量文件中使用tint属性,如:

    <vector ..vector properties..
            android:tint="@color/vector_color_selector">
                  <path ..path properties../>
    </vector>
    

    (您还可以在AppCompatImageView中使用app:tint属性)

    现在,您的vector_color_selector文件应该具有您想要的颜色,如:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true" android:color="@color/blue"/>
        <item android:state_pressed="true" android:color="@color/black"/>
        .
        .
        etc
    </selector>
    

    如果以前的答案不适合你,我希望这可以帮助别人。说明显而易见,但我必须说你还需要在gradle中设置vectorDrawables.useSupportLibrary = true,使用AppCompatImageView并使用app:srcCompat或setImageDrawable + AppCompatResources.getDrawable来避免使用vector compat库的任何麻烦。


    0
    投票

    正如Harish Gyanani在评论中所说的那样使用AppCompatImageView而不是ImageView,它适用于我。

    Official docs


    56
    投票

    为了补充这里的一些答案:对VectorDrawables的向后兼容支持需要付出代价,并不适用于所有情况。

    它在哪些情况下有效?我已经让this diagram提供帮助(对支持库23.4.0有效至​​少25.1.0)。

    VectorDrawable cheatsheet


    29
    投票

    尝试使用:

    imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null));
    

    您不必以这种方式添加AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

    只需使用VectorDrawableCompat对您的矢量绘图进行充气,就可以了。


    25
    投票

    我们遇到了同样的问题。在Kitkat上看不到矢量drawables。我通过将AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);添加到活动的onCreate方法来解决了这个问题。

    在此之前不要忘记添加:

    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    

    并为您使用矢量drawable的视图调用setImageResource。我的观点是ImageButton。我有Android SDK构建工具版本23.0.3


    14
    投票

    很抱歉迟到了,但这个答案可能会帮助那些想要启用标志的用户AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);所有活动。

    1.创建一个扩展到Application(android.app.Application)的类

    public class MyApplicationClass extends Application
    {
        @Override
        public void onCreate()
        {
            super.onCreate();
        }
    }
    

    2.转到Manifest.xml并将以下行添加到标记中

    <application
        android:name=".MyApplicationClass"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ...
    </application>
    

    3.在MyApplicationClass.java中的onCreate上添加以下代码

    // This flag should be set to true to enable VectorDrawable support for API < 21
    static
    {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
    

    MyApplicationClass.java的完整代码

    import android.app.Application;
    import android.support.v7.app.AppCompatDelegate;
    
    /**
    * Created by Gaurav Lonkar on 23-Dec-17.
    */
    
    public class MyApplicationClass extends Application
    {
        // This flag should be set to true to enable VectorDrawable support for API < 21
        static
        {
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        }
    
        @Override
        public void onCreate()
        {
            super.onCreate();
        }
    }
    

    8
    投票
    defaultConfig {
      vectorDrawables.useSupportLibrary = true
    }
    

    在app.gradle中使用它

    然后使用AppCompatDrawableManager来设置setDrawable和getDrawable。适合我


    7
    投票

    android:drawableLeft这样的地方支持矢量绘图在支持库23.3中被禁用。它在Google+上宣布:

    由于23.2.0 / 23.2.1版本中的实现中发现的问题,我们决定删除允许您在Lollipop前设备上使用矢量drawable的功能。使用app:srcCompat和setImageResource()继续工作。

    问题链接:

    但是,如果你能解决这些问题,可以在23.4中使用AppCompatDelegate.setCompatVectorFromResourcesEnabled()重新启用此功能。

    如果你对这是如何工作感到好奇,那么最好的人就是Chris Banes,他是这个功能的创作者。他详细解释了on his blog


    4
    投票

    更改

    imageView.setImageResource(R.drawable.drawable_image)
    

    imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image));
    

    如果你想在xml中使用vectordrawable,请使用:

    app:srcCompat="@drawable/drawable_image"
    

    1
    投票

    我很久以前遇到过类似的问题,设置不起作用

    vectorDrawables.useSupportLibrary = true

    仅在我创建“mipmap”文件夹和使用的代码时才有效

    imageView.setImageResource (R.mipmap.drawable_image)

    它有更多的信息here

    © www.soinside.com 2019 - 2024. All rights reserved.