在android中如何画一条虚线

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/c_he_n/article/details/80416812

先看效果图

我们要画一个虚线,作为界面中的分割。
这里写图片描述

代码实现

在drawable中新建一个,dot_line.xml文件,按照下面代码进行配置,其实dashGap是线段之间的间隔大小,而dashWidth是线段的大小,按照自己的需求进行修改。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1px"
        android:color="@color/libres_select_btn_bg_gray"
        android:dashGap="2dp"
        android:dashWidth="2dp" />
</shape>

在视图中是这样使用,其中android:layerType这个属性是兼容4.0的。

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dot_line" 
        android:layerType="software" />

注意点

在TextView中, android:layout_height采用”wrap_content”属性,而是写具体的大小,我在项目中写1px,编译后不能显示。我采用的是TextView,而不是View,因为在采用View是不显示,若是采用View ,android:layout_height=”1dp”就可以显示,不能设置为“wrap-content”,不知道为什么,回头研究下。

猜你喜欢

转载自blog.csdn.net/c_he_n/article/details/80416812