1. 程式人生 > >android在XML中畫橫線、豎線和虛線

android在XML中畫橫線、豎線和虛線

一、畫橫線

<View
          android:layout_width="match_parent"
          android:layout_height="@dimen/border_line"
          android:background="#000000" />

二、畫豎線

<View
          android:layout_width="1dp"
          android:layout_height="match_parent"
          android:background="#000000" />

三、畫虛線

  • 在drawable中新建一個dash_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke
    android:color="#000000"
    android:dashWidth="3dp"
    android:dashGap="5dp"/>
    <!--width:線條的高度
    color:顏色
    dashWidth:破折線的寬度
    dashGap:破折線間空隙的寬度
    如果這裡設定了width,xml中android:layout_height的值必須比它大。不設定就預設為0
    -->
</shape>
  • 在佈局檔案中使用
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layerType="software"
        android:background="@drawable/dash_line"/>