close

紀錄一下 Android layout 怎拉分隔線,首先我用的 OS 是 Windows 10,Android 開發的 IDE 如下:

1.jpg

 

分隔線的主要用法就是使用 <View></View>:

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

上述是水平分隔線,寬度為 3dp (layout_height),顏色是黑色 (#000000)。

 

下面我再舉個實際的應用,我拉一個 LinearLayout,擺入兩個 TextView,並且在兩個 TextView 中置入分隔線:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上方文字"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <View
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:background="#000000"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="parent" />
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下方文字"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.648" />
</LinearLayout>

 

實際如下圖:

Screenshot_20201104-140604.png

 

如果不需要分隔線把整個護面切開,可以用 layout_marginLeft 與 layout_marginRight 讓分隔線短一點:

<View
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="#000000"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintEnd_toEndOf="@+id/textView2"
    app:layout_constraintTop_toBottomOf="parent" />

打完收工

 

arrow
arrow

    葛瑞斯肯 發表在 痞客邦 留言(0) 人氣()