我在旧版Android上使用矢量图时遇到了一些问题。我需要在每次活动启动时更改可绘制对象,应该加载相应的SVG文件。
这是我的图层列表:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="oval">
<size android:height="50dp" android:width="50dp"/>
<!-- fill color -->
<solid android:color="@color/white" />
</shape>
</item>
<item
android:id="@+id/avatar"
android:drawable="@drawable/dog" //I need to change this at run time
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp"/>
在我的活动中,我正在使用
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
目前我正在使用层叠绘图来选择XML文件并更改SVG,但我遇到了兼容性问题,因为layerDrawable.setDrawble()
仅适用于API级别> = 23
layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.seekbar_thumb);
Drawable avatar = (Drawable) ContextCompat.getDrawable(this, getUserAvatarId());
layerDrawable.setDrawable(0, avatar);
AppCompatResources.getDrawable()
来获取它。 - android developer