Android 虚线出不来的问题解决方法

来源:互联网 发布:大数据时代到来 编辑:程序博客网 时间:2024/06/08 06:10

有些时候需求要我们画一条虚线,效果图是这样的


我们一般会这样写

dashed_line.xml 文件

<?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/huise"        android:dashGap="8dp"        android:dashWidth="8dp" /></shape>


然后在布局文件中引用

<View android:layout_width="match_parent"    android:layout_height="1.5dp"    android:background="@drawable/dashed_line"    android:layout_marginTop="20dp"/>
却发现出来的结果是这样的,没错,直接一条实线就出来了
这个是因为从安卓3.0开启,提供了硬件加速功能,导致的这个bug。我们只要把硬件加速关闭就可以了。但是关闭整个app或者关闭整个activity的硬件加速,好像不太合适。所以我们只设置我们的虚线不启用硬件加速

所以在引用的地方加上这么一句

<View android:layout_width="match_parent"    android:layout_height="1.5dp"    android:background="@drawable/dashed_line"
    android:layerType="software"
    android:layout_marginTop="20dp"/>
就可以了。效果就出来了


0 0
原创粉丝点击