我已经编写了一个带有自定义属性的自定义复合视图。其中一个自定义属性是可绘制的,而我希望使用的文件是Vector。
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
val iconDrawable = typedArray.getDrawable(R.styleable.CustomView_icon_drawable)
我一直得到一个XmlPullParserException:二进制XML文件行#1:无效的可绘制标记向量
为什么会这样呢?
发布于 2017-03-20 19:35:02
发布于 2017-03-18 15:08:47
支持库
向量绘图支持从Android4.4 (API 20)开始。因此,如果将build.gradle文件中的最低API级别(build.gradle)设置为小于20,请确保使用的是支持库。
若要启用支持库,请在应用程序级别的build.gradle中添加以下行
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
自定义属性定义
在attrs.xml中将属性定义为引用类型:
<declare-styleable name="CustomView">
<attr name="icon_drawable" format="reference" />
</declare-styleable>
获取可绘制的实例
最后,为了能够在您的.xml布局文件中获得指定可绘制的实例,获取一个可绘制的资源ID,并使用支持类ContextCompat
创建此可绘制文件的实例。
final int drawableResId = typedArray.getResourceId(R.styleable.CustomView_icon_drawable, -1);
final Drawable drawable = ContextCompat.getDrawable(getContext(), drawableResId)
https://stackoverflow.com/questions/42863198
复制相似问题