在Android UI制作中,经常会需要一些线条作为分隔线,一般做个width或height为1dp的view就可以解决了,如果需要虚线,则需要在drawable目录自定义xml进行绘制了,一般xml如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="@color/white" android:dashWidth="5dp" android:dashGap="2dp" /> </shape>
不过如果需要一条竖虚线,就麻烦很多。
首先,同样定义xml文件,不过要旋转90度,这样就是竖的了:
<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="90" android:toDegrees="90"> <shape android:shape="line"> <stroke android:width="1dp" android:color="@color/white" android:dashWidth="5dp" android:dashGap="2dp" /> </shape> </rotate>
1)在view的宽度设大一些,然后设置marginLeft 和marginRight 为负值,就不会影响到旁边的view了
<View android:background="@drawable/dot_line_white" android:layout_marginLeft="-10dp" android:layout_marginRight="-10dp" android:layerType="software" android:layout_width="50dp" android:layout_height="match_parent"/>
注意:设置时必须设置layerType为software,否则手机显示不会显示出虚线。