Android通过layer-list为控件绘制一边或多边边线的方法

layer-list:将多个图片按照代码顺序层叠起来的效果

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#ff0000" />
        </shape>
    </item>
    <!--这里的right和bottom表示的是这一层图片的右边距和下边距,当然还有left和top-->
    <item android:right="10px" android:bottom="10px">
        <shape>
            <solid android:color="#00ff00" />
        </shape>
    </item>
</layer-list>

效果:
这里写图片描述

主要是第二个item的四个边距属性的应用,依次我们可以堆叠很多层,甚至让控件的四个边线呈现不同的颜色都是可以做到的

比如这样(right_gray_border_bg.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#ff0000" />
        </shape>
    </item>
    <item android:top="10px">
        <shape>
            <solid android:color="#00ff00" />
        </shape>
    </item>
    <item android:left="10px" android:top="10px">
        <shape>
            <solid android:color="#0000ff" />
        </shape>
    </item>
    <item android:left="10px" android:top="10px" android:bottom="10px">
        <shape>
            <solid android:color="#49A3D5" />
        </shape>
    </item>
    <item android:left="10px" android:top="10px" android:bottom="10px" android:right="10px">
        <shape>
            <solid android:color="#B14C7E" />
        </shape>
    </item>
</layer-list>

效果:
这里写图片描述

引用:

android:background="@drawable/right_gray_border_bg"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值