其效果如下图所示:
关于layer-list的进一步解释见SDK文档,如下:
Layer List
A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
Each drawable is represented by an <item> element inside a single <layer-list> element.
- file location:
res/drawable/filename .xml
The filename is used as the resource ID.- compiled resource datatype:
- Resource pointer to a
LayerDrawable. - resource reference:
- In Java:
R.drawable.filename
In XML:@[package :]drawable/filename - syntax:
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list
- xmlns:Android="http://schemas.android.com/apk/res/android" >
- <item
- android:drawable="@[package:]drawable/drawable_resource"
- android:id="@[+][package:]id/resource_name"
- android:top="dimension"
- android:right="dimension"
- android:bottom="dimension"
- android:left="dimension" />
- </layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</layer-list>
- elements:
- <item android:drawable="@drawable/image" />
<item android:drawable="@drawable/image" />
To avoid scaling, the following example uses a <bitmap> element with centered gravity:
- <item>
- <bitmap android:src="@drawable/image"
- android:gravity="center" />
- </item>
<item>
<bitmap android:src="@drawable/image"
android:gravity="center" />
</item>
-
- example:
- XML file saved at
res/drawable/layers.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item>
- <bitmap android:src="@drawable/android_red"
- android:gravity="center" />
- </item>
- <item android:top="10dp" android:left="10dp">
- <bitmap android:src="@drawable/android_green"
- android:gravity="center" />
- </item>
- <item android:top="20dp" android:left="20dp">
- <bitmap android:src="@drawable/android_blue"
- android:gravity="center" />
- </item>
- </layer-list>
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <bitmap android:src="@drawable/android_red" android:gravity="center" /> </item> <item android:top="10dp" android:left="10dp"> <bitmap android:src="@drawable/android_green" android:gravity="center" /> </item> <item android:top="20dp" android:left="20dp"> <bitmap android:src="@drawable/android_blue" android:gravity="center" /> </item> </layer-list>
Notice that this example uses a nested
<bitmap>element to define the drawable resource for each item with a "center" gravity. This ensures that none of the images are scaled to fit the size of the container, due to resizing caused by the offset images.This layout XML applies the drawable to a View:
This layout XML applies the drawable to a View:
- <ImageView
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:src="@drawable/layers" />
<ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/layers" />
The result is a stack of increasingly offset images:
- see also:
