在CompoundDrawable中使用带有SDK< 21的SVG

时间:2016-06-29 18:48:07

标签: android svg textview

我在我的Android应用程序中使用SVG作为我的ImageView的appSrc,使用android:src属性以向后兼容(SDK< 21)。 但是现在我尝试在我的TextView Compound Drawables(drawableXXX属性)中使用它们,当我使用带有KitKat的设备时出现多个错误(当我使用app:srcCompat而不是{{1}时,我遇到的错误相同})。

  

引起:android.view.InflateException:二进制XML文件行#261:错误导致类

有没有人知道在复合抽屉中使用它们的方法?

3 个答案:

答案 0 :(得分:2)

截至目前,您无法从xml属性中添加VectorDrawable,在Android开发人员TextView

中,该功能仅限于app:srcCompat

但是,您可以使用VectorDrawableCompat.create(Resources, int, Theme)以编程方式执行此操作,然后使用TextView#setCompoundDrawables将其作为复合可绘制添加到TextView

见:
developer.android.com/reference/android/support/graphics/drawable/VectorDrawableCompat.html developer.android.com/reference/android/widget/TextView.html

答案 1 :(得分:0)

我有同样的问题,我发现只有两个选择:

1)以编程方式设置drawable或创建自定义视图

2)从build.gradle中删除“vectorDrawables.useSupportLibrary = true”

  

这将导致Android Studio在编译时为minSdkVersion低于API 21的应用生成PNG,同时在API 21+设备上使用向量,允许您以额外的APK大小为代价保留相同的代码。

答案 2 :(得分:0)

this article中所述,您可以为此问题创建一个小技巧。像这样创建layer-list可绘制文件(ic_working_image.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_your_image_name"/>
</layer-list>

然后像以前一样在TextView上使用带有drawableXXX属性的新drawable。