Android layer-list的使用

layer是在PS中非常常用的功能,是用来多个图层堆叠显示的。接下来做一个简单的例子:

Android layer-list的使用_第1张图片Android layer-list的使用_第2张图片

点击左边条目 显示不同的背景。
正常时候的背景

<?xml version="1.0" encoding="utf-8"?>
<layer-list  xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape android:shape="rectangle">
            <solid android:color="#e5e5e5" />
        </shape>
    </item>
    //底部上移1dp  右边左移1dp  显示黑色线条
    <item android:right="1dp" android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#f2f3f5"/>
        </shape>
    </item>
</layer-list>

点击时的背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list  xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
        <shape android:shape="rectangle">
            <solid android:color="#e5e5e5" />
        </shape>
    </item>
    //底部上移1dp  右边没有
    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>
</layer-list>

再写一个selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/normal"></item>

</selector>

然后设置为背景,就可以实现上图的效果。

你可能感兴趣的:(android)