Android开发(34) xml drawable实现listview分割线
- 2020 年 3 月 16 日
- 筆記
需求
我需要自定义 listView的分割线,而这个分割线是由两条线组成的,在使用xml drawable时遇到了困难。
画两条线是为了实现 凹陷的效果,在绘图中一条暗线紧跟着一条明显会给人视觉上产生明显的“沟”的感觉。 因为我的背景是透明(背景渐变)的,直接用图片来实现效果不好。
效果图:

思路
使用 layer-list 来实现。 layer-list 可以包含多个item,每个item堆叠在一起。 layer-list 的item可以设置 间距,使用 android:bottom 这样的属性来实现。
具体做法:
1.先绘制第一条线。 2.第二条线设定间距,距离头部1px,这样才不会完全重叠 3.两条线堆叠在一起
具体代码如下
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:bottom="0px" android:left="0px" android:right="0px" android:top="0px"> <shape android:shape="rectangle" > <size android:height="1px" android:width="1px" /> <solid android:color="#A18249" /> </shape> </item> <item android:bottom="0px" android:left="0px" android:right="0px" android:top="3px"> <shape android:shape="rectangle" > <size android:height="2px" android:width="2px" /> <solid android:color="@android:color/white" /> </shape> </item> </layer-list>
参考
http://stackoverflow.com/questions/14436641/understanding-androids-layer-list