android - 当drawable 资源是Vector Drawable 时,如何通过TypedArray 获取Drawable 对象?

标签 android android-vectordrawable

我已经编写了一个带有自定义属性的自定义复合 View 。自定义属性之一是可绘制对象,我希望使用的文件是矢量可绘制对象。

val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
val iconDrawable = typedArray.getDrawable(R.styleable.CustomView_icon_drawable)

我不断收到 XmlPullParserException:二进制 XML 文件行 #1:无效的可绘制标签向量

这是为什么?

最佳答案

已解决。

我需要执行以下操作:

val drawableResId = typedArray.getResourceId(R.styleable.CustomView_icon_drawable, -1);
val drawable = AppCompatResources.getDrawable(getContext(), drawableResId)

归功于 pskinkcreck寻求解决方案。

关于android - 当drawable 资源是Vector Drawable 时,如何通过TypedArray 获取Drawable 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42863198/

相关文章:

c# - 在 Xamarin Forms 中的应用程序之间共享数据

android:包含布局时出现问题

android - 可以使用 GradientColor 完全在 XML 中定义填充或描边的渐变吗?

java - 如何在(Expandable)ListAdapter下设置ImageView的色调?

android - 以编程方式为支持向量着色

android - 如何在偷看自定义 android 可穿戴设备通知上添加图标?

java - 我的 OnNavigationItemSelectedListener kotlin android 有什么问题吗?

Android - 如何使用 onKeyDown?

android - 带有 TabLayout 的 ViewPager 问题与 AnimatedVectorDrawable

android - 如何将可绘制的矢量转换为PNG文件?