Android画一条虚线

首先在drawable里面创建一个bg_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="#999999"
        android:dashGap="5dp"
        android:dashWidth="5dp"
        />
</shape>

效果是这个样子滴:

看样子好像已经成功了,最后在把这个作为background加进去,

代码如下

<View
        android:id="@+id/dotted_line"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@drawable/bind_no_card_bg"/>

结果在真机上跑一圈,发现显示的竟然是一条实线,在好几台手机上试都是这样,都是一条实线,后来查资料发现需要加上一句话
android:layerType=”software” .

完整代码如下:

<View
        android:id="@+id/dotted_line"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@drawable/bind_no_card_bg"
        android:layerType="software"/>
这样画出来的就是一条虚线啦!

猜你喜欢

转载自blog.csdn.net/yushuangping/article/details/80005876