1. 程式人生 > >android xml中設定水平虛線及豎直虛線

android xml中設定水平虛線及豎直虛線

廢話不多說。。。上圖

1. 水平虛線 在這裡插入圖片描述

2. 豎直虛線 在這裡插入圖片描述 在專案的res資料夾drawable下面去建立:

水平虛線:line_stroke

<?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="@color/line"   ----》虛線顏色
        android:dashGap="4dp"    ----》虛線間隔寬度
        android:dashWidth="6dp"/>  ---》虛線的寬度
</shape>

豎直虛線:line_vertical

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-600dp"
        android:right="-600dp">  ---》 讓其居中顯示
        <rotate android:drawable="@drawable/line_stroke"   ----》引用水平虛線的drawable
            android:visible="true"   
            android:fromDegrees="90"  ---》設定旋轉度90度
            />
    </item>
</layer-list>

最後一步。。。。xml

       <TextView
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layerType="software"    ---》記得新增屬性
            android:background="@drawable/line_vertical"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_centerVertical="true"
            android:layerType="software"  ---》記得新增屬性
            android:background="@drawable/line_stroke" />

總結,肯定有人疑問為什麼要用textView去顯示虛線呢,不用View去展示 之前在專案中用8.0之後,在專案中有時候會神奇報錯:

錯誤呢,之前出錯的,現在找不到了

我講述一下吧。。。。

主要報錯的是繪製的豎直虛線 它會報你res下drawable中的line_vertical中的no drawable