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

Android 布局管理實戰:LinearLayout、TableLayout 與 RelativeLayout

編輯:中階開發

布局決定了 Activity 介面上各個元件的排列方式。本文以實作角度出發,重點說明 LinearLayout 的嵌套與權重分配、TableLayout 的表格排版,以及 RelativeLayout 的相對定位屬性,並附上可直接套用的 XML 範例。

先認識常用的元件屬性

在動手排版之前,先熟悉幾個會反覆出現的屬性:

  • android:id:為元件指定唯一 ID。
  • android:text:指定元件顯示的文字,建議盡量引用 strings.xml 中的字串資源。
  • android:gravity:指定元件內容的對齊位置,例如居中、靠右等。
  • android:textSize:指定字體大小。
  • android:background:指定背景色,採 RGB 命名法。
  • android:layout_width / android:layout_height:指定元件寬與高。
  • android:padding*:指定內邊距,即內容與邊框之間的距離。
  • android:singleLine:設為 true 時,內容會在同一行顯示。

LinearLayout:線性排列與權重分配

LinearLayout 顧名思義是以「一條線」的方式排列元件,可分為水平(horizontal)與垂直(vertical)兩型。它最大的特色是 android:layout_weight 權重屬性:把父容器的空間依比例分配給子元件。例如兩個元件都設 layout_weight="1",就等於把空間分成兩份、各占一份。

由於 LinearLayout 可以自由嵌套,只要靈活搭配水平與垂直方向,就能組出相當複雜的版面。以下範例展示一個垂直的外層容器,內含一個水平的彩色橫列與一個垂直的文字清單:

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="red"
            android:gravity="center_horizontal"
            android:background="#aa0000"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="green"
            android:gravity="center_horizontal"
            android:background="#00aa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="blue"
            android:gravity="center_horizontal"
            android:background="#0000aa"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="row one"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <TextView android:text="row two"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

TableLayout:表格式的排列

TableLayout 以表格形式排版,透過 android:stretchColumns 指定哪些列可以拉伸填滿剩餘寬度(索引從 0 開始)。每一列以 TableRow 表示,列中的每個子元件即為一個儲存格:

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

    <TableRow>
        <TextView android:text="@string/row1_column1"
            android:background="#aa0000" android:padding="3dip" />
        <TextView android:text="@string/row1_column2"
            android:gravity="center_horizontal"
            android:background="#00aa00" android:padding="3dip" />
        <TextView android:text="@string/row1_column3"
            android:gravity="right"
            android:background="#0000aa" android:padding="3dip" />
    </TableRow>

    <TableRow>
        <TextView android:text="@string/row2_column1"
            android:padding="3dip" />
        <TextView android:text="@string/row2_column2"
            android:gravity="right" android:padding="3dip" />
    </TableRow>
</TableLayout>

LinearLayout 與 TableLayout 混用

布局容器之間可以互相嵌套。例如下方範例在外層用 LinearLayout 切分出上下兩塊(各占 weight=1),下半部再嵌入一個 TableLayout,就能在同一個畫面裡同時運用兩種排版邏輯,達到更細緻的版面安排。

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

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="red"
            android:gravity="center_horizontal"
            android:background="#aa0000"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="green"
            android:gravity="center_horizontal"
            android:background="#00aa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0">
            <TableRow>
                <TextView android:text="@string/row1_column1" android:padding="3dip" />
                <TextView android:text="@string/row1_column2"
                    android:gravity="right" android:padding="3dip" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</LinearLayout>

RelativeLayout:以方位進行定位

RelativeLayout 以元件之間的相對方位來排版,透過一組 layout_ 屬性描述位置關係。常用屬性可分為兩類。

第一類是相對於「其他元件」:

  • android:layout_above:將本元件底部置於指定 ID 元件之上。
  • android:layout_below:將本元件頂部置於指定 ID 元件之下。
  • android:layout_toLeftOf:將本元件右緣與指定 ID 元件的左緣對齊。
  • android:layout_toRightOf:將本元件左緣與指定 ID 元件的右緣對齊。
  • android:layout_alignTop / android:layout_alignBottom:將本元件的頂部/底部與指定 ID 元件對齊。
  • android:layout_alignLeft / android:layout_alignRight:將本元件的左緣/右緣與指定 ID 元件對齊。
  • android:layout_alignBaseline:讓本元件與指定 ID 元件的基線對齊。

第二類是相對於「父容器」:

  • android:layout_alignParentLeft / android:layout_alignParentRight:與父容器的左/右緣對齊。
  • android:layout_alignParentTop / android:layout_alignParentBottom:與父容器的頂部/底部對齊。
  • android:layout_centerHorizontal / android:layout_centerVertical:置於父容器水平/垂直方向的中央。
  • android:layout_centerInParent:置於父容器正中央。

以下範例做出一個典型的「輸入框 + OK / Cancel 按鈕」版面:文字標籤在上,輸入框緊接其下,OK 按鈕靠右對齊並位於輸入框下方,Cancel 按鈕則排在 OK 左側、頂部對齊。

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

    <TextView android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:" />

    <EditText android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label" />

    <Button android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10px"
        android:text="OK" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

小結

LinearLayout 靠嵌套與 weight 靈活切分畫面,TableLayout 適合表格化資料,RelativeLayout 則以相對關係精準定位。三者若能搭配使用,幾乎可以應付絕大多數的介面排版需求。建議動手改一改範例中的屬性與權重,觀察畫面變化,對布局的理解會更深刻。

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