Android竖虚线绘制

来源:互联网 发布:python外星人入侵图片 编辑:程序博客网 时间:2024/05/16 07:35

在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>

然后在需要画虚线的地方使用该drawable作为背景即可。

不过如果需要一条竖虚线,就麻烦很多。

首先,同样定义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>

另外,在使用该drawable时,宽度不能设为1dp,因为这个宽度是旋转前的虚线长度,如果设为1dp,则看不出虚线了,所以需要一点小技巧:

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"/>

2)使用FrameLayout等布局方式,将虚线view置于其他view之上。

注意:设置时必须设置layerType为software,否则手机显示不会显示出虚线。

0 0
原创粉丝点击