AppCompat 23.3 支持向量不再有效?

AppCompat 23.3 Support Vectors no longer work?

我使用了 Support Library 23.2 中添加的支持向量可绘制对象以及 AppCompat。我在 app:srcCompatStateListDrawable 中使用矢量可绘制对象,因此我可以将它们与 android:drawableLeft 一起用于我的 TextView。

自从升级到 23.3.0 版本的 AppCompat 后,只有 app:srcCompat 中的矢量可用。每当我以另一种方式引用它时,我都会得到

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.package.name/.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class Button
 ...
 Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class Button
 ...
 Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #14: invalid drawable tag vector
  at android.graphics.d

现在有什么变化导致我的支持向量可绘制对象在某些情况下失败?

更新: 他们在支持库 23 中再次启用它:

For AppCompat users, we’ve added an opt-in API to re-enable support Vector Drawables from resources (the behavior found in 23.2) via AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); - keep in mind that this still can cause issues with memory usage and problems updating Configuration instances, hence why it is disabled by default.

勾选this link: 23.4.0 available now
---------------------------------------------- ----------

根据 release announcement for Android Support Library 23.3:

For AppCompat users, we’ve decided to remove the functionality which let you use vector drawables from resources on pre-Lollipop devices due to issues found in the implementation in version 23.2.0/23.2.1 [ https://code.google.com/p/android/issues/detail?id=205236, https://code.google.com/p/android/issues/detail?id=204708 ]. Using app:srcCompat and setImageResource() continues to work.

所以这是预期的行为变化。对于 srcCompat.

未处理的任何情况,您都必须使用非矢量图形

如果您想在 API 21 之前继续使用矢量,您可以删除行

vectorDrawables.useSupportLibrary = true

(如果您使用 23.2 blog post 中所示的 1.5 Gradle 插件,则为等效项)。

这将导致 Android Studio 在编译时为 minSdkVersion 小于 API 21 的应用程序生成 PNG,同时在 API 21+ 设备上使用矢量,允许您保留与 23.2.1 相同的代码,但要增加 APK 大小。

在不使用

的情况下将矢量用作 compoundDrawables(例如用于 textview)
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

这导致记录在案的高内存使用率,只需使用

膨胀向量
Drawable drawable = AppCompatResources.getDrawable( getContext(), R.drawable.vector_resID );
if( drawable != null ) drawable.setBounds( 0, 0, iconSize, iconSize );
TextViewCompat.setCompoundDrawablesRelative( textView, null, null, drawable, null);

这就是 navDrawer 的工作方式

在支持库 23.2.0 中添加了对 pre-Lollipop 的 VectorDrawable 支持,然后在 23.3.0 中部分删除了。在 23.4.0 及更高版本(至少 25.1.0)中,删除的部分又回来了,但在可选标志后面(因为它带有价格)。

总结:在支持库 23.4.0 到至少 25.1.0 中,您可以让 VectorDrawable 工作在某些情况下.

我做了this diagram来帮忙