Use layer-list to define complex graphics in Android

During the development process, sometimes the background graphics are a little more complicated, which cannot be achieved by shape alone. At this time, you need to use layer-list to draw. The principle of layer-list is similar to the relative layout of RelativeLayout, which realizes more complex graphics by superimposing multiple items.
The effect achieved in the following example is: a rounded rectangle with a blank area of ​​12dp length on the left.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <!-- 图形填充色 -->
            <solid android:color="#00000000" />
        </shape>
    </item>
    <!--
      item中有4个属性(类似于layout布局中的margin属性):
      left:距离左侧的偏移量。
      right:距离右侧的偏移量。
      top:距离上侧的偏移量。
      bottom:距离下侧的偏移量。
    -->
    <item
        android:left="12dp">
        <shape>
            <!-- 圆角 -->
            <corners android:radius="5dp" />
            <!-- 图形填充色 -->
            <solid android:color="#ff4f84d3" />
            <!-- 边框 -->
            <stroke
                android:width="@dimen/btn_stroke_width"
                android:color="#ff1d6cdb" />
        </shape>
    </item>
</layer-list>

Note: The items in the layer-list are in order, the first item is located at the bottom, the next item covers the previous item, and so on. If the order is wrong, the effect is completely different.

Guess you like

Origin blog.csdn.net/chenzhengfeng/article/details/106855064