支持库VectorDrawable资源$ NotFoundException
我正在使用devise支持库版本23.4.0。 我已经启用了gradle标志
defaultConfig { vectorDrawables.useSupportLibrary = true } 我正在使用生成工具版本23.0.2,但仍然获得Kitkat或更低的ResourcesNotFoundexception。
 这是当我使用android:drawableLeft或imageView.setImageResource(R.drawable.drawable_image) 
是的,我把这个放在我使用drawable的每一个活动上
  static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } 
这是支持库的错误吗?
我花了3个独立的东西来使用支持库23.4.0:
- 
将此添加到build.gradle defaultConfig { vectorDrawables.useSupportLibrary = true }
- 
将以下内容添加到应用程序类的onCreate中 AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
- 
对于您在其中设置vector可绘制replace的所有xml视图 android:src同 app:srcCompat并在代码中replace为: imageView.setImageResource(...);同 imageView.setImageDrawable(...);
为了补充一些答案: VectorDrawables向后兼容的支持带有价格,并且在所有情况下都不起作用 。
在哪些情况下工作? 我已经使这个图帮助(对支持库23.4.0有效至less25.1.0)。
  
 
 我们有同样的问题。  Kitkat上没有可见的vector绘图。 我通过添加AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);来解决这个问题AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 到活动的onCreate方法。 
在此之前,不要忘记添加:
 defaultConfig { vectorDrawables.useSupportLibrary = true } 
并调用setImageResource为您使用vector绘制的视图。 我的看法是ImageButton。 我有Android SDK构build工具版本23.0.3
尝试使用:
 imageView.setImageDrawable(VectorDrawableCompat.create(getResources(), drawableRes, null)); 
 您不必添加AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 这条路。 
只需使用VectorDrawableCompat充气你的vector绘图,你就全部设置好了。
 defaultConfig { vectorDrawables.useSupportLibrary = true } 
在app.gradle中使用这个
 然后使用AppCompatDrawableManager setDrawable和getDrawable。 为我工作 
 在android:drawableLeft这样的地方支持vector绘制,在支持库23.3中被禁用。 在Google+上宣布: 
我们已经决定删除由于在23.2.0 / 23.2.1版本的实现中发现的问题而使您可以使用棒棒糖前设备上的资源中的vector绘图的function。 使用app:srcCompat和setImageResource()继续工作。
问题链接:
- https://code.google.com/p/android/issues/detail?id=205236
- https://code.google.com/p/android/issues/detail?id=204708
但是,如果您可以忍受这些问题,则可以在23.4中使用AppCompatDelegate.setCompatVectorFromResourcesEnabled()重新启用此function。
如果您好奇这是如何工作的,最好的学习者是Chris Banes,他创作了这个function。 他在他的博客上详细解释。
更改
 imageView.setImageResource(R.drawable.drawable_image) 
至
 imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.drawable_image)); 
如果你想在xml中使用vectordrawable,使用这个:
 app:srcCompat="@drawable/drawable_image" 
我很久以前也有类似的问题,但是没有通过设置工作
 vectorDrawables.useSupportLibrary = true 
只有当我创build“mipmap”文件夹,并使用的代码工作
 imageView.setImageResource (R.mipmap.drawable_image) 
这里有更多的信息
  
 
  充气Drawable的 
 这个支持库中的`VectorDrawable`和`AnimatedVectorDrawable`可以用这种方式来夸大: 
-  调用静态getDrawable()方法:
//这只会使用<vector>作为根元素膨胀一个drawable VectorDrawable.getDrawable(context,R.drawable.ic_arrow_vector); //这只会使用<animated-vector>作为根元素膨胀drawable AnimatedVectorDrawable.getDrawable(context,R.drawable.ic_arrow_to_menu_animated_vector); //这将膨胀任何可绘制的,并将自动回落到api 21+设备上的棒棒糖实现 ResourcesCompat.getDrawable(context,R.drawable.any_drawable);
  如果在Java代码中绘制Drawable,则build议始终使用ResourcesCompat.getDrawable()因为这样可以处理棒棒糖回退。  这允许系统cachingDrawable ConstantState,因此效率更高。 
 该库有以下变形(双向)animation: 
 正如你所看到的,我在API 16手机上制作了上面的图片: 
 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 
 如果将它与应用程序模块的build.gradle dependencies (通常在文件末尾)合并,这将解决您的问题(直到API 14 ): 
 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' } 
 不要把你的载体drawable-anydpi ,旧的设备不支持 
 把它们放在drawable