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

Android 開發實戰(九):五大佈局全解析——線性、相對、框架、絕對與表格

編輯:關於 Android 開發

介面是使用者對一個 App 的第一印象,而佈局決定了介面的骨架。這一節把 Android 最基礎的五種佈局逐一拆開來看,順便說清楚該在什麼場合用哪一種。

一、線性佈局(LinearLayout)

線性佈局把子元件排成一條線,方向有兩種:水平與垂直。它的好處是簡單直觀,缺點也很明確——不太方便隨意調整單一元件的位置。所以實務上很少單用,多半是線性與相對佈局互相嵌套。

上圖同時示範了水平與垂直兩個方向,可以清楚看到元件如何依序排開。幾個常用屬性:

android:orientation="horizontal"    // 水平排列
android:orientation="vertical"      // 垂直排列
android:layout_weight="1"           // 按比例分配剩餘空間
android:gravity="center_horizontal" // 元件在佈局內的水平對齊方式

layout_weight 是線性佈局最實用的屬性:同一層級的元件若都設為 1,剩餘空間就平均分配。下面用嵌套方式組出「上半水平、下半垂直」的結構:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_weight="2">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/jay" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="雨松 MOMO"
            android:background="#FF0000"
            android:textColor="#000000"
            android:textSize="18dip" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="水平方向" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="雨松 MOMO"
            android:background="#FF0000"
            android:textColor="#000000"
            android:textSize="18dip" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="垂直方向" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="雨松 MOMO" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image" />
    </LinearLayout>
</LinearLayout>

二、相對佈局(RelativeLayout)

相對佈局是 Android 佈局家族裡最強的一員:可用屬性最多,能表達的關係也最豐富。更重要的是,它的座標取值都是相對的——元件相對於父容器或兄弟元件定位,而不是釘死在某個絕對位置。手機螢幕解析度五花八門,相對佈局天生就適合做自適應。

android:layout_alignParentRight="true"   // 與父容器右側對齊
android:layout_below="@id/re_edit_0"     // 位於 re_edit_0 下方
android:layout_toLeftOf="@id/re_image_0" // 位於 re_image_0 左側
android:layout_alignTop="@id/name"       // 與 name 的上緣對齊
android:layout_marginRight="30dip"        // 位移像素值
<?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="fill_parent">

    <EditText
        android:id="@+id/re_edit_0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="雨松 MOMO"
        android:layout_alignParentRight="true" />

    <ImageView
        android:id="@+id/re_iamge_0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/jay"
        android:layout_below="@id/re_edit_0"
        android:layout_alignParentRight="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="努力學習"
        android:textColor="#000000"
        android:textSize="18dip"
        android:layout_toLeftOf="@id/re_iamge_0" />

    <EditText
        android:id="@+id/re_edit_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="雨松 MOMO"
        android:layout_alignParentBottom="true" />

    <ImageView
        android:id="@+id/re_iamge_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
        android:layout_above="@id/re_edit_1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="努力工作"
        android:textColor="#000000"
        android:textSize="18dip"
        android:layout_toRightOf="@id/re_iamge_1"
        android:layout_above="@id/re_edit_1" />
</RelativeLayout>

使用相對佈局時有個順序上的注意事項:被引用的元件必須先宣告,或者至少要有 id,否則會拿到錯誤的參照。

三、框架佈局(FrameLayout)

框架佈局的規則很單純:所有子元件疊在同一個位置上,後畫的蓋住先畫的,最上層永遠是最後一個。它適合需要堆疊的場合,例如相片上面浮一層文字。

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/g" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="雨松 MOMO"
        android:background="#FF0000"
        android:textColor="#000000"
        android:textSize="18dip" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image"
        android:layout_gravity="bottom" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="快樂生活每一天喔"
        android:layout_gravity="bottom" />
</FrameLayout>

圖中的 ImageView 先畫,之後的 TextView 與 EditText 就疊在它上面。搭配 android:layout_gravity 可以讓每個子元件各自靠齊不同邊,不至於全擠在左上角。

四、絕對佈局(AbsoluteLayout)

絕對佈局讓你直接指定每個元件在螢幕上的 X、Y 座標。聽起來很自由,但筆者不建議使用——理由還是那句話:Android 手機解析度五花八門,寫死座標的介面換一台機器就散了。它和框架佈局一樣,後畫的會蓋住先畫的。

android:layout_x="50dip"
android:layout_y="30dip"
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/f"
        android:layout_x="100dip"
        android:layout_y="50dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="當前座標點 x = 100dip y = 50dip"
        android:background="#FFFFFF"
        android:textColor="#FF0000"
        android:textSize="18dip"
        android:layout_x="50dip"
        android:layout_y="30dip" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/h"
        android:layout_x="50dip"
        android:layout_y="300dip" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="當前座標點 x = 50dip y = 300dip"
        android:background="#FFFFFF"
        android:textColor="#FF0000"
        android:textSize="18dip"
        android:layout_x="30dip"
        android:layout_y="280dip" />
</AbsoluteLayout>

了解它的原理有價值——特別是讀懂別人的老程式碼時——但新專案裡基本用不到。

五、表格佈局(TableLayout)

表格佈局以列為單位組織內容,透過 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">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/g"
        android:layout_gravity="center" />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dip">
        <TextView android:text="姓名" android:gravity="left" />
        <TextView android:text="電話" android:gravity="right" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FFFFFF" />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dip">
        <TextView android:text="雨松" android:gravity="left" />
        <TextView android:text="15810463139" android:gravity="right" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dip">
        <TextView android:text="小可愛" android:gravity="left" />
        <TextView android:text="15810463139" android:gravity="right" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dip">
        <TextView android:text="姓名" android:gravity="left" />
        <TextView android:text="性別" android:gravity="right" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FFFFFF" />

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dip">
        <TextView android:text="雨松 MOMO" android:gravity="left" />
        <TextView android:text="男" android:gravity="right" />
    </TableRow>
</TableLayout>

那兩段 View 是分隔線的小技巧:高度給 2dip、塞一個背景色,就能在列與列之間畫出一條線。

該選哪一種?

五種佈局的基本用法講完了,最後還是回到實務選擇:

  • 相對佈局是首選。屬性最豐富,絕大部分版面都能用它完成,而且天生適應不同螢幕。
  • 線性佈局適合方向明確的清單式排列,尤其是搭配 layout_weight 做比例分割。
  • 框架佈局用在需要堆疊的場合。
  • 表格佈局處理表格型資料。
  • 絕對佈局基本只在維護老專案時才會遇到。

真正的關鍵其實是嵌套:把這幾種佈局組裝起來,才能做出既好看又能適配各種螢幕的介面。不必執著於單一佈局,依實際情況選用最省事的那一種就好。

源碼下載地址:https://vdisk.weibo.com/s/aa2C2

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