Introduction to basic usage of layer-list

The layer-list is a type of Drawable in Android that is used to define a Drawable object made up of multiple layers. Each layer can be a color, shape, or image.

Layer-list allows multiple layers to be stacked on top of each other in a specific order, creating complex Drawable effects. These layers can be different shapes, colors, and images, and their position, size, scale, and transparency can be controlled by setting different layer attributes.

Here is an example code for a layer-list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/colorPrimary" /> <!-- 第一个图层,使用颜色作为背景 -->
    <item
        android:drawable="@drawable/ic_launcher_background"
        android:gravity="center" /> <!-- 第二个图层,使用图片,并设置居中显示 -->
</layer-list>

In this example, the layer-list includes two layers. The first layer uses a color as the background, while the second layer uses an image as the background and is set to be displayed in the center.

Supported attributes of layer-list include:

  1. android:drawable: specifies the Drawable resource for a layer, which can be a color, shape, or image.
  2. android:id: a unique identifier assigned to a layer.
  3. android:gravity: specifies the alignment of the layer within the parent container, such as center, top, bottom, etc.
  4. android:left, android:top, android:right, and android:bottom: specify the boundaries of the layer within the parent container.

By setting these properties, various different Drawable effects can be created. The layer-list is commonly used for creating buttons, backgrounds, and state selectors.

bannerAds