以下是我的LayerList的簡化版本。它在一個項目中有一個位圖來防止縮放。我想以編程方式更改此位圖的drawable。有沒有辦法做到這一點?更改在LayerListDrawable中可繪製的位圖
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item"
android:left="165dp"
android:top="245dp" >
<bitmap
android:id="@+id/bitmap"
android:src="@drawable/tag_de_01"
android:gravity="top|left" />
</item>
</layer-list>
我嘗試沒有成功如下:
Resources res = getResources();
LayerDrawable layerDrawable = (LayerDrawable) res.getDrawable(R.drawable.layer_list_drawable);
Drawable newDrawable = (Drawable)res.getDrawable(R.drawable.something);
// works, but scales the image because the drawable of the item is set instead the one of the bitmap
layerDrawable.setDrawableByLayerId(R.id.item, newDrawable);
// doesn't work at all
layerDrawable.setDrawableByLayerId(R.id.bitmap, newDrawable);
我有同樣的問題。有人可以幫忙嗎? – Lobo 2012-05-06 12:46:20
你能想出答案嗎? – 2015-11-09 13:05:51