虚线的xml文件,dashlineshape.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<!-- 显示一条虚线,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp 时,为实线 -->
<stroke android:width="1dp" android:color="#D5D5D5"
android:dashWidth="2dp" android:dashGap="2dp" />
<!-- 虚线的高度 -->
<size android:height="1dp" />
</shape>
设置Imageview的方式为
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@drawable/dashlineshape"
android:layerType="software" />
这样设置之后,发现虚线并不显示,查阅了百度
1.从android3.0开始,安卓关闭了硬件加速功能,所以就不能显示了,所以就是在 AndroidManifest.xml,或者是在activity中把硬件加速的功能关掉就可以了android:hardwareAccelerated="false"或者是view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
发现并不能解决,最后发现,Imageview的高度必须大于虚线的高度,虚线才能显示,后将imageview的属性设置如下
<ImageView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@drawable/dashlineshape"
android:layerType="software" />
网友评论