开始在网上查找了一番,始终没有实现不知道怎么回事?特此记录一下;
先在xml定义:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:dashGap="3dp" android:dashWidth="1dp" android:color="@color/line_color" android:width="1dp"/>
<size android:height="1dp"/>
</shape>
width:线段的高度
color:线段的颜色
dashWidth:线段宽度
dashGap:线段之间间隔宽度
在布局中:
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@drawable/dash_line"
android:layerType="software" />
在4.0的设备上,虚线会变成实线,我们需要加入 android:layerType=”software”属性,但是这样设置可能会无效果,如果全部按照上面的方式,应该会显示,为什么会出现这样的情况呢,经过测试,
shape要定义size高度至少为1(如果没有写,那么android:width="1dp"这个如果是1的话,那View中android:layout_height="2dp"这里是2要大于xml中width的值1dp,到此ok
),view的布局的高度要大于1至少为2