Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 版面配置全解:五種 Layout 的用途與屬性對照

編輯:Android 開發實例

Android 的畫面結構由 Layout 決定。早期版本提供了五種主要容器,各有其定位邏輯:線性排列、層層覆疊、表格分割、絕對座標與相對關係。理解每種容器的特性,才能選對工具而不是硬湊。

先搞懂兩個基礎屬性

layout_width 與 layout_height 決定尺寸,有三種寫法:fill_parent 表示跟著父容器走、wrap_content 表示依內容大小決定、或直接給一個具體數值(如 100px)。這兩個屬性幾乎每個 View 都必須設定,漏掉會導致版面配置失敗。

五種 Layout 的特性

  • LinearLayout(線性配置):以 orientation 決定排列方向,vertical 為垂直堆疊、horizontal 為水平並排;gravity 則控制內容在容器內的對齊方式,常用值有 top、bottom、left、right、center。
  • FrameLayout(層疊配置):以左上角為原點,所有子元件一層覆蓋一層,後加入者在上。常用於浮動圖示、自訂彈出層。
  • TableLayout(表格配置):以 TableRow 定義列,列內每個元件即為一格。三個欄位控制屬性分別是 collapseColumns(隱藏欄)、stretchColumns(拉伸填滿剩餘空間)、shrinkColumns(自動收縮避免溢出畫面)。
  • AbsoluteLayout(絕對定位):以 layout_x、layout_y 指定左上角座標。看似精準,但不同螢幕尺寸會嚴重走位,實務上幾乎不建議使用。
  • RelativeLayout(相對定位):以「相對關係」定位,例如 layout_centerInParent 置中、layout_marginLeft 設定左邊距、layout_below 放到某元件下方、layout_alignRight 與某元件右緣對齊。它能在多種螢幕上維持比例,是早期最實用的容器。

版面配置範例 res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
 layout_width  - 寬。fill_parent:跟著父元素走;wrap_content:跟著本身內容走;也可直接指定 px 值
 layout_height - 高。同上
-->
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:gravity="right"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <!-- FrameLayout - 層疊式配置,以左上角為起點,一層覆蓋一層 -->
    <FrameLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="FrameLayout">
        </TextView>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="Frame Layout">
        </TextView>
    </FrameLayout>

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

    <!--
    TableLayout - 表格式配置。
      TableRow - 表格內的列,列內每個元件算一列(欄)
      collapseColumns - 需隱藏的欄索引,多個以 , 隔開
      stretchColumns  - 需拉伸填滿可用空間的欄索引
      shrinkColumns   - 需自動收縮以免其他欄被擠出螢幕的欄索引
    -->
    <TableLayout android:id="@+id/TableLayout01"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:collapseColumns="1">
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView android:layout_width="wrap_content"
                android:layout_weight="1" android:layout_height="wrap_content"
                android:text="行1列1" />
            <TextView android:layout_width="wrap_content"
                android:layout_weight="1" android:layout_height="wrap_content"
                android:text="行1列2" />
            <TextView android:layout_width="wrap_content"
                android:layout_weight="1" android:layout_height="wrap_content"
                android:text="行1列3" />
        </TableRow>
        <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="行2列1" />
        </TableRow>
    </TableLayout>

    <!--
    AbsoluteLayout - 絕對定位配置。
      layout_x - x 座標,以左上角為原點
      layout_y - y 座標,以左上角為原點
    -->
    <AbsoluteLayout android:layout_height="wrap_content"
        android:layout_width="fill_parent">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="AbsoluteLayout"
            android:layout_x="100px"
            android:layout_y="100px" />
    </AbsoluteLayout>

    <!--
    RelativeLayout - 相對定位配置。
      layout_centerInParent - 置於容器水平與垂直中央(類似還有 layout_centerHorizontal、layout_alignParentLeft 等)
      layout_marginLeft      - 相對於容器左緣的距離
      layout_below           - 放到指定元件下方
      layout_alignRight      - 與指定元件右緣對齊
    -->
    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TextView android:layout_width="wrap_content" android:id="@+id/abc"
            android:layout_height="wrap_content" android:text="centerInParent=true"
            android:layout_centerInParent="true" />
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="marginLeft=20px"
            android:layout_marginLeft="20px" />
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="xxx"
            android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
    </RelativeLayout>

</LinearLayout>

字串資源 res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello Layout</string>
    <string name="app_name">webabcd_layout</string>
</resources>

Activity 端

版面配置寫好後,Activity 只需要一行 setContentView 就把畫面掛上,這正是「畫面描述與程式邏輯分離」的價值。

package com.webabcd.layout;

import android.app.Activity;
import android.os.Bundle;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

選用建議

實務上的優先順序是:能用 LinearLayout 解決就用它(效能最好、階層最淺);需要跨欄跨列對齊時用 RelativeLayout 或 TableLayout;FrameLayout 只留給單純的覆疊需求;AbsoluteLayout 除了相容舊專案,不建議再用。此外,版面階層越深,measure 與 layout 的開銷越大,保持淺而扁平是效能的基本原則。

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