android布局中画线的方法

时间:2022-01-18 20:00:57

1.自定义View画线


下面介绍几种简单的方法
2.textView和View画直线

    1. <TextView
    2. android:layout_width="match_parent"
    3. android:layout_height="1dp"
    4. android:background="#bfbfbf" />

    1. <View
    2. android:layout_width="match_parent"
    3. android:layout_height="1dp"
    4. android:background="#FF909090"/>

background为设置直线的颜色
但是也只能画直线

3.自定义shape文件

dotted_line.xml文件
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:shape="line" >
    4. <stroke
    5. android:dashGap="3dp"
    6. android:dashWidth="6dp"
    7. android:width="1dp"
    8. android:color="#bfbfbf" />
    9. <!-- 虚线的高度 -->
    10. <size android:height="1dp" />
    11. </shape>
其中,破折线的宽度为dashWith,破折线之间的空隙的宽度为dashGap,当dashGap=0dp时,为实线

在你的xml布局中引用
    1. <LinearLayout
    2. android:layout_width="match_parent"
    3. android:layout_height="2dp"
    4. android:background="@drawable/dotted_line"
    5. android:layerType="software"/>

效果:
android布局中画线的方法