Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 佈局詳解(下):Layout Params 與常見佈局的實際用法

編輯:關於 Android 程式設計

寫 XML 佈局時,你一定見過 layout_width 這類以 layout_ 開頭的屬性。它們並不是 View 自身的屬性,而是父容器用來安排子元素的「佈局參數」。搞懂這層關係,再回頭看 LinearLayout 與 RelativeLayout 的種種屬性,思路就清晰多了。

一、Layout Parameters 是怎麼運作的

這些屬性的作用是讓子 View 適配它的父 ViewGroup。每個 ViewGroup 類別都實作了一個繼承自 ViewGroup.LayoutParams 的內部類,裡面定義了該容器特有的、用來描述子 View 尺寸與位置的屬性。

換句話說:父容器決定你能寫哪些 layout 屬性,而你的設定又會繼續影響你自己的子元素。下面這段 XML 就是很好的說明:

<!-- 根佈局是 LinearLayout -->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#0f0"
    android:orientation="vertical">

    <!-- LinearLayout 的子 View 是 RelativeLayout -->
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f00">

        <TextView
            android:id="@+id/textView1InRL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2InRL"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:text="TextView2"
            android:layout_toRightOf="@id/textView1InRL" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#fff"
            android:text="TextView3"
            android:layout_below="@id/textView2InRL" />
    </RelativeLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1InLinearLayout" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2InLinearLayout" />
</LinearLayout>

這裡 RelativeLayout 的 layout_width、layout_height 由父容器 LinearLayout 解讀;而它自己的 layout_toRightOf、layout_below 則是給三個 TextView 用的。同一個 View,站在不同位置就有不同身份。

二、線性佈局 LinearLayout

把子元素沿單一方向排列,可以垂直也可以水平。當內容總長度超出螢幕時,就需要捲軸來輔助。

加捲軸的做法:用 ScrollView 包一層

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- 這裡放要捲動的內容 -->
        </LinearLayout>
    </ScrollView>
</LinearLayout>

要留意 ScrollView 只能有一個直接子元素,所以通常都會再包一層 LinearLayout。

一個完整的登入畫面範例

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="帳號:" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="密碼:" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="30dp"
                    android:text="登入" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:text="註冊" />
            </LinearLayout>

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="500dp"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LinearLayout 大小超過螢幕的測試" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

這個例子刻意把 ImageView 的高度設成 500dp,讓總高度超過螢幕,用來驗證捲軸是否生效。注意 LinearLayout 可以嵌套,內層改成 horizontal 就能做出「兩個按鈕並排」的效果。

三、相對佈局 RelativeLayout

顧名思義,它靠「相對關係」定位。你可以讓子元素彼此對齊(例如按鈕 A 在文字 B 下方),也可以讓子元素相對父容器定位(水平居中、靠右上等)。

若完全不設定任何規則,所有子 View 都會預設堆在左上角。

登入頁面的相對佈局寫法

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- layout_centerHorizontal 讓標題水平居中 -->
    <TextView
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="20sp"
        android:text="登入介面" />

    <!-- layout_below 讓輸入框排在標題下方,layout_marginTop 留出 24dp 間距 -->
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/login"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:hint="username"
        android:ems="10">
        <requestFocus />
    </EditText>

    <!-- editText2 緊接在 editText1 下方 -->
    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:ems="10"
        android:hint="password"
        android:inputType="textPassword" />

    <!-- layout_alignRight 讓取消鈕與 editText2 右緣對齊 -->
    <Button
        android:id="@+id/cancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText2"
        android:layout_alignRight="@id/editText2"
        android:text="取消" />

    <!-- layout_toLeftOf 讓確定鈕排在取消鈕左側 -->
    <Button
        android:id="@+id/confirmButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText2"
        android:layout_toLeftOf="@id/cancelButton"
        android:text="確定" />
</RelativeLayout>

幾個實用提醒:@+id/ 代表建立新 id,@id/ 代表引用既有 id,兩者混用是 RelativeLayout 常見的錯誤來源。另外 RelativeLayout 的依賴關係必須是「被引用者先宣告」,否則會找不到資源,這一點在複雜佈局裡特別容易踩到。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved