Android中绘制虚线

389 阅读1分钟

利用shape绘制一条虚线

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

    <!--
    dashWidth 是指虚线中每一个实线的宽度
    dashGap 是指虚线中每一个间隔的宽度
    dashWidth 和dashGap 中有一个为0,那么这条线就是一条实线。
    width是指这条线的宽度,在横的虚线上体现出来就是这条线的竖直方向的高度。
    把这个drawable作为一个view的背景时,需要设置view的layerType 为software,不然在真机上显示出来是一条实线。
    作为背景的view的layout_height要超过drawable的stroke的width,不然显示不出来。

     <View
            android:id="@+id/dashedLineView"
            android:layout_width="match_parent"
            android:background="@drawable/dashed_line_white"
            android:layerType="software"
            android:layout_height="4dp"/>
-->

    <stroke
        android:width="@dimen/dp_2"
        android:color="@color/color_black"
        android:dashWidth="10dp"
        android:dashGap="10dp" />
    
</shape>

参考:blog.51cto.com/u_10847930/…