<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="line" xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="@color/border_line"
android:dashWidth="2dp"
android:dashGap="2dp"/>
<size android:height="2dp"/>
</shape>
画虚线的代码
其中dashGap是空隙宽度,dashWidth是线的宽度,当dashGap也就是空隙宽度为0时,显示一条实线。然后在background或src中引用,但在android 4.0以上版本中会有BUG,发现不管dashGap设置多大,显示的都是一条实线。
解决方法:
关闭硬件加速。
可以在AndroidManifest.xml时的Application标签加上android:hardwareAccelerated=”false”,这样整件应用都关闭了硬件加速,虚线可以正常显示,但是,关闭硬件加速对性能有些影响,会感觉明显比没关卡。
也可以给虚线的view单独关闭硬件加速:
line.setLayerType(View.LAYER_TYPE_SOFTWARE,NULL);