Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 版面配置入門:View、ViewGroup 與 LinearLayout 嵌套實作

使用者介面是人与手機溝通的橋樑,也是使用者對一個 App 的第一印象。Android 的 UI 由 View 與 ViewGroup 兩類物件構成,兩者都是 View 的子公司別。

  • View:螢幕上最基本的呈現單位。它的子類別被統稱為 widgets(控制項),例如 TextView、EditText、Button,提供文字輸入框、按鈕這類完整可用的 UI 元件。
  • ViewGroup:View 的擴充,本身不畫內容,負責「容納並排列子 View」。延伸它還能做出由多個子 View 組成的複合控制項。
  • ViewGroup 最常見的用途是當版面配置管理器:LinearLayout(線性)、TableLayout(表格)、RelativeLayout(相對)、FrameLayout(框架)等。

LinearLayout 是什麼

顧名思義就是「線性配置」:標籤底下所有子元素會依 android:orientation 的值,逐個沿著水平或垂直方向排成一列。

它最大的特性是簡單,最大的限制也是簡單——只能排一條線。想做上下兩列、每列內部又是左右並排的表單,就必須靠嵌套:外層垂直、內層水平。

實作:一個含帳號、密碼與按鈕的登入表單

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 第一列:帳號 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/username" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- 第二列:密碼 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/userpass" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <!-- 第三列:按鈕群,用 TableLayout 橫向並排 -->
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="*">

        <TableRow>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/login" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cancel" />
        </TableRow>
    </TableLayout>

</LinearLayout>

這份配置裡值得注意的幾點

  • 外層 orientation="vertical" 決定三個區塊上下堆疊;兩個內層 LinearLayout 用 horizontal 讓文字標籤與輸入框左右並排。這就是嵌套的價值。
  • android:stretchColumns="*" 是 TableLayout 的關鍵屬性,星號代表所有欄位平均分配剩餘寬度,兩個按鈕才會均分而對齊。
  • 標籤用 wrap_content、輸入框用 fill_parent(新版建議寫 match_parent),是表單類版面的標準搭配,能讓輸入框吃滿該列剩餘空間。
  • 版面中的文字不要寫死,一律抽到 res/values/strings.xml,方便日後多語系與重複使用。

選哪個 Layout

  • LinearLayout:單純的直向或橫向排列,效能好,但層數一多就容易嵌套過深。
  • TableLayout:需要欄位對齊的表格型資料。
  • RelativeLayout:以「相對其他元件或父容器」定位,能有效壓平層級。
  • 實務建議:嵌套越少越好。層數過深會直接拖慢 measure/layout 的效能,能用 RelativeLayout 或 ConstraintLayout 攤平的,就不要一層疊一層。
Copyright © Linux教程網 All Rights Reserved