-->

Unable to understand layer-list

2019-07-22 15:11发布

问题:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"
        android:left="4dp">
        <shape
            android:shape="oval">
            <solid android:color="#ff0000" />
        </shape>
    </item>
    <item>
        <shape
            android:shape="oval">
            <stroke android:width="2dp"
                android:color="#ff0000"/>
        </shape>
    </item>
</layer-list>

taken from here:

https://stackoverflow.com/a/36003935/6007737

How is it giving me a ring shape?

How layer-list works and What do top, right, bottom and left attributes of item tag do?

Cant we just use ring shape ?Why go for oval shape to make ring shape?

回答1:

A layer list is drawable, called sequence of other drawables with <item> tag. Here from your question the first <item> is inner oval shape & top, bottom, right & left are insets given to that item (just same as padding). Try to give width & height to first you can see inner oval shape with 4dp padding from outer oval.

Refer to this link for more detail about layer drawable http://developer.android.com/reference/android/graphics/drawable/LayerDrawable.html

Yes you can use ring shape to draw a ring like:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="15dp"
    android:thickness="10dp"
    android:useLevel="false">
    <solid android:color="#ff0000" />

</shape>