语法:在drawalbe/drawable-layer.xml中
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:color/white" /> <item android:drawable="@drawable/logo_overlay" /> </layer-list>
<resources> <style name="LookupTheme" parent="@android:style/Theme.Light.NoTitleBar"> <item name="android:windowBackground">@drawable/drawable-layer</item> </style> </resources>在java代码中引用:
Resources resources = getResources(); Drawable[] layers = new Drawable[2]; layers[0] = r.getDrawable(R.drawable.white); layers[1] = r.getDrawable(R.drawable.logo_overlay); LayerDrawable layerDrawable = new LayerDrawable(layers) ((ImageView) findViewById(R.id.imageview)).setImageDrawable(layerDrawable);
首先创建一个drawalbe/login_head.xml的文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/user_faceback_drawable" android:drawable="@drawable/faceback" /> <item android:id="@+id/user_face_drawable" android:drawable="@drawable/h001" android:left="10.0dip" android:top="18.0dip" android:right="25.0dip" android:bottom="35.0dip" /> </layer-list>
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.cwj); Drawable[] array = new Drawable[3]; array[0] = new PaintDrawable(Color.BLACK); //黑色 array[1] = new PaintDrawable(Color.WHITE); //白色 array[2] = new BitmapDrawable(bm); //位图资源 LayerDrawable ld = new LayerDrawable(array); //参数为上面的Drawable数组 ld.setLayerInset(1, 1, 1, 1, 1); //第一个参数1代表数组的第二个元素,为白色 ld.setLayerInset(2, 2, 2, 2, 2); //第一个参数2代表数组的第三个元素,为位图资源 mImageView.setImageDrawable(ld);