Tom*_*ila 13 android android-vectordrawable
我编写了一个带有自定义属性的自定义复合视图.其中一个自定义属性是drawable,我希望使用的文件是Vector Drawable.
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
val iconDrawable = typedArray.getDrawable(R.styleable.CustomView_icon_drawable)
Run Code Online (Sandbox Code Playgroud)
我一直得到一个XmlPullParserException:二进制XML文件行#1:无效的drawable标签向量
为什么是这样?
Tom*_*ila 15
解决了.
我需要做以下事情:
val drawableResId = typedArray.getResourceId(R.styleable.CustomView_icon_drawable, -1);
val drawable = AppCompatResources.getDrawable(getContext(), drawableResId)
Run Code Online (Sandbox Code Playgroud)
jpa*_*act 12
Android 4.4(API 20)支持矢量drawables.因此,如果build.gradle文件中的最低API级别(minSdkVersion)设置为小于20,请确保使用支持库.
要启用支持库,请在您的应用级build.gradle中添加以下行:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
Run Code Online (Sandbox Code Playgroud)
在attrs.xml中将您的属性定义为引用类型:
<declare-styleable name="CustomView">
<attr name="icon_drawable" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
最后,为了能够在.xml布局文件中获取指定drawable的实例,获取可绘制的资源ID并使用支持类ContextCompat创建此Drawable的实例
final int drawableResId = typedArray.getResourceId(R.styleable.CustomView_icon_drawable, -1);
final Drawable drawable = ContextCompat.getDrawable(getContext(), drawableResId)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3233 次 |
| 最近记录: |