先创建 dashed.xml
再将 dashed.xml 作为 View 的背景
<?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/gainsboro"
android:dashGap="6dp"
android:dashWidth="4dp" />
<size android:height="1dp"/>
</shape>
再将 dashed.xml 作为 View 的背景
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@drawable/dashed"
android:layerType="software"/>
注意:
1. View 的 height 要比虚线的高度大一点,否则显示不出来;
2. Android 4.0以上设备要设置 layerType 属性为 software ,否则显示的效果是一条实线。