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

安卓六大基礎佈局詳解:線性、相對、表格、網格、絕對與框架

編輯:Android 程式設計入門

佈局檔(layout)決定了畫面的骨架。Android 內建的六大基礎佈局各有適用場景,選對容器往往比寫一堆樣式屬性更省事。

1. 線性佈局(LinearLayout)
2. 相對佈局(RelativeLayout)
3. 表格佈局(TableLayout)
4. 網格佈局(GridLayout)
5. 絕對佈局(AbsoluteLayout)
6. 框架佈局(FrameLayout)

一、線性佈局 LinearLayout

最直覺的容器:子元件依單一方向排列,靠 android:orientation 指定垂直或水平。搭配 layout_weight 可以按比例分配剩餘空間,這是它最實用的特性。

<?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"
    android:padding="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000FF"
        android:gravity="center_horizontal|center_vertical"
        android:text="線性佈局"
        android:textSize="20sp"/>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:background="#0000FF"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:background="#00FF00"
            android:text="內容一" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#F7F709"
            android:text="內容二" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:text="按一" android:onClick="click" />
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:text="按二" android:onClick="click"/>
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:text="按三" android:onClick="click"/>
        <Button android:layout_width="0dp" android:layout_height="wrap_content"
            android:layout_weight="1" android:text="按四" android:onClick="click"/>
    </LinearLayout>
</LinearLayout>

用 weight 分配寬度時,記得把 layout_width 設為 0dp,而不是 wrap_content,否則各元件會先依內容佔位再瓜分剩餘空間,比例就不準了。

実作一個登入頁:

<?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" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal|center_vertical"
        android:text="線性佈局登錄頁面"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="賬號:"
            android:textSize="15sp" />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:orientation="horizontal" >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密碼:"
            android:textSize="15sp" />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="記住賬號" />
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="記住密碼" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginTop="15dp"
        android:orientation="horizontal" >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="click"
            android:text="登錄"
            android:textSize="10sp" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15sp" />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="click"
            android:text="注冊"
            android:textSize="10sp" />
    </LinearLayout>
</LinearLayout>

二、相對佈局 RelativeLayout

相對佈局允許子元件彼此參照,例如「在 A 的下方」「與 B 底部對齊」「靠父容器右側」。由於能大幅減少嵌套層數,在效能上通常比多層 LinearLayout 更友善。代價是屬性繁多,複雜畫面容易難以維護。

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

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="相對佈局登錄頁面"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/labelAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:layout_marginLeft="80px"
        android:layout_marginTop="30dp"
        android:text="賬號:"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/etAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/labelAccount"
        android:layout_marginLeft="130px"
        android:layout_marginRight="50px" />

    <TextView
        android:id="@+id/labelPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etAccount"
        android:layout_marginLeft="80px"
        android:layout_marginTop="20dp"
        android:text="密碼:"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/labelPassword"
        android:layout_marginLeft="130px"
        android:layout_marginRight="50px" />

    <CheckBox
        android:id="@+id/cbAccount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/etPassword"
        android:layout_marginLeft="100px"
        android:layout_marginTop="20dp"
        android:text="記住賬號" />

    <CheckBox
        android:id="@+id/cbPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/cbAccount"
        android:layout_marginLeft="280px"
        android:layout_marginRight="50px"
        android:text="記住密碼" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/cbPassword"
        android:layout_marginLeft="100px"
        android:layout_marginRight="290px"
        android:layout_marginTop="25dp"
        android:text="登錄"
        android:textSize="14sp" />

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignBottom="@id/btnLogin"
        android:layout_marginLeft="290px"
        android:layout_marginRight="100px"
        android:text="注冊"
        android:textSize="14sp" />
</RelativeLayout>

注意所有參照用的 id 都必須先宣告,且不能形成循環依賴,否則會在執行期出錯。

三、表格佈局 TableLayout

TableLayout 以列(TableRow)為單位組織內容,android:stretchColumns 可指定哪些欄位要拉伸填滿剩餘寬度。適合資料型、需要對齊的畫面。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:stretchColumns="2" >

    <TableRow>
        <TextView android:layout_width="50dp" android:layout_height="wrap_content"
            android:gravity="center" android:text="序號" android:textSize="20sp" />
        <TextView android:layout_width="60dp" android:layout_height="wrap_content"
            android:gravity="center" android:text="書名" android:textSize="20sp" />
        <TextView android:gravity="center" android:text="內容" android:textSize="20sp" />
        <TextView android:layout_width="60dp" android:layout_height="wrap_content"
            android:gravity="center" android:text="作者" android:textSize="20sp" />
    </TableRow>
    <View android:layout_height="1.5dp" android:background="#00FF00" />
    <TableRow>
        <TextView android:gravity="center" android:text="1" />
        <TextView android:gravity="center" android:text="西游記" />
        <TextView android:gravity="center" android:text="取經" />
        <TextView android:gravity="center" android:text="鄭晨" />
    </TableRow>
    <View android:layout_height="1.5dp" android:background="#00FF00" />
    <TableRow>
        <TextView android:gravity="center" android:text="2" />
        <TextView android:layout_column="2" android:gravity="center" android:text="孫悟空大鬧天空" />
    </TableRow>
    <View android:layout_height="1.5dp" android:background="#00FF00" />
    <TableRow>
        <TextView android:gravity="center" android:text="3" />
        <TextView android:layout_column="1" android:gravity="center" android:text="盤龍" />
        <TextView android:layout_column="3" android:gravity="center" android:text="鄭晨" />
    </TableRow>
    <View android:layout_height="1.5dp" android:background="#00FF00" />
</TableLayout>

layout_column 可以讓某個元件直接跳到指定欄位,中間跳過的格子會留空,這在合併儲存格的視覺效果上很好用。

四、網格佈局 GridLayout

GridLayout 以 rowCount / columnCount 定義網格,layout_columnSpan 可讓元件跨越多欄,很適合計算機按鈕這類規則版面。

<?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"
    android:padding="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center"
        android:text="計算機"
        android:textSize="30sp" />

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:columnCount="4"
        android:rowCount="6" >

        <EditText
            android:id="@+id/tvResult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="4"
            android:layout_gravity="fill_horizontal"
            android:editable="false"
            android:focusable="false"
            android:gravity="right"
            android:text="0"
            android:textSize="20sp" />

        <Button android:text="AC" android:onClick="acClick"/>
        <Button android:text="+/-" />
        <Button android:text="%" />
        <Button android:text="+" />
        <Button android:text="7" />
        <Button android:text="8" />
        <Button android:text="9" />
        <Button android:text="x" android:onClick="anClick" />
        <Button android:text="4" />
        <Button android:text="5" />
        <Button android:text="6" android:onClick="bnClick" />
        <Button android:text="-" />
        <Button android:text="1" android:onClick="onClick" />
        <Button android:text="2" />
        <Button android:text="3" />
        <Button android:text="+" />
        <Button android:layout_columnSpan="2"
            android:layout_gravity="fill_horizontal" android:text="0" />
        <Button android:text="." />
        <Button android:text="=" />
    </GridLayout>
</LinearLayout>

Activity 部分(僅示範幾個按鍵的處理方式):

public class GridLayoutActivity extends Activity {
    private EditText editText;
    private boolean lastClickIsNumber = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        editText = (EditText) findViewById(R.id.tvResult);
    }

    public void acClick(View v) {
        editText.setText("0");
    }

    public void onClick(View v) {
        appendText("1");
    }

    public void anClick(View v) {
        appendText("x");
    }

    public void bnClick(View v) {
        appendText("6");
    }

    private void appendText(String text) {
        if (!lastClickIsNumber) {
            editText.setText(text);
        } else {
            String oldContent = editText.getText().toString().trim();
            editText.setText(oldContent + text);
        }
        lastClickIsNumber = true;
    }
}

五、絕對佈局 AbsoluteLayout

用 layout_x 與 layout_y 直接指定座標。寫起來直觀,但完全綁死在某種螢幕尺寸上,換一台解析度不同的裝置就會跑位,因此官方早已標記為 deprecated。理解原理即可,不建議用於正式專案。

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp">

    <TextView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_x="100dp" android:layout_y="50dp" android:text="你好嗎?"/>
    <TextView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_x="50dp" android:layout_y="100dp" android:text="我很好!"/>
    <TextView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_x="200dp" android:layout_y="100dp" android:text="真的嗎?"/>
    <TextView android:layout_width="100dp" android:layout_height="100dp"
        android:layout_x="100dp" android:layout_y="200dp" android:text="真的!"/>
</AbsoluteLayout>

六、框架佈局 FrameLayout(補充)

FrameLayout 讓所有子元件疊在同一個位置,最上層的會蓋住下面的,常用於浮層、Fragment 容器或自定義畫面的底圖。它的結構最簡單,也因此在效能上最輕。

選型建議

單向排列用 LinearLayout;需要減少嵌套時用 RelativeLayout(新版專案也可考慮 ConstraintLayout);表格化資料用 TableLayout;規則格子用 GridLayout;層疊遮罩用 FrameLayout;絕對佈局則只當歷史知識。實務上真正影響效能的往往不是選了哪個容器,而是嵌套了幾層——能壓平就壓平。

下一篇接著介紹安卓的基礎控制項。

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