Android中绘制虚线的方法

APP开发中我们经常会用到虚线的绘制,这里提供一种虚线绘制的方法

首先drawable下编写一个xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/holo_green_light"/>
    <stroke android:width="1dp"
        android:color="@android:color/black"
        android:dashGap="10dp"
        android:dashWidth="20dp"/>
</shape>

但如果就这样引入View的背景会发现还是一条直线。这里需要我们关闭硬件加速android:layerType="software",完整代码如下

<View
   android:id="@+id/dotted_line"
   android:layout_width="match_parent"
   android:layout_height="5dp"
   android:layout_marginLeft="13dp"
   android:layout_marginRight="13dp"
   android:background="@drawable/bg_dotted_line"
   android:layerType="software"
  />

猜你喜欢

转载自blog.csdn.net/weixin_38322371/article/details/89028480