Android shape 画虚线

一、画法

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<size android:height="1dp"/>
<stroke
android:width="3px"
android:color="#dedede"
android:dashWidth="10px"
android:dashGap="10px" />
</shape>

二、虚线不显示

这个在layout预览的时候就可以看出,不必运行到手机或者模拟器上。
原因是View的高度不大于 虚线的高度;

<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="center_vertical"
android:background="@drawable/comment_share_dash_line"
android:layerType="software" />

如果在标签中设置了android:width,则在标签中android:layout_height的值必须大于android:width的值,否则虚线不会显示。如果不设置,默认android:width为0。

三、虚线显示为实线

第一种解决方法:

代码中添加:

line.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

第二种解决方法:

在xml中添加

android:layerType="software"
作者

AriaLyy

发布于

2018-07-05

许可协议

CC BY-NC-SA 4.0

评论