尝试学习一些新事物,但无法弄清楚这一点,感谢您的帮助。鉴于谷歌文档中的这个简单代码:
层.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/first_image">
<bitmap android:src="@drawable/android_red"
android:gravity="center" />
</item>
<item android:id="@+id/second_image" android:top="10dp" android:left="10dp">
<bitmap android:src="@drawable/android_green"
android:gravity="center" />
</item>
<item android:id="@+id/third_image" android:top="20dp" android:left="20dp">
<bitmap android:src="@drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
和 main.xml:
<ImageView
android:id="@+myImageView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/layers" />
问题:如何以编程方式引用图层列表中的一个可绘制对象,以便将其更改为不同的可绘制对象?
谢谢。
所以现在我有我想换成变量的新可绘制对象myImage
正确吗?现在我如何把它放到图层列表中?我假设我使用findDrawableByLayerId
and setDrawableByLayerId
,但究竟如何?或者我可能做错了!帮助!
我想做什么?更改显示的可绘制对象。例如,假设我有另一个名为“android_purple”的drawable,我将如何将它切换为上面图层列表示例中的“android_green”drawable?
编辑:
到目前为止,我的知识有限(这不起作用,我得到setDrawableByLayerId
未定义的错误):
Resources res = getApplicationContext().getResources();
BitmapDrawable newImage = (BitmapDrawable) res.getDrawable(R.drawable.android_purple); //Get replacement image, can't use LayerDrawable or get error.
boolean layer_drawable_changed = (setDrawableByLayerId((R.id.second_image), newImage)); //Set new drawable? Error on this line.