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

Android 四大基本佈局詳解:LinearLayout、RelativeLayout、FrameLayout、TableLayout

編輯:關於 Android 程式設計

再花俏的介面,拆到底層也不過是這四種佈局的組合與巢狀。搞懂各自的定位規則,排版就不再是試錯。

  • LinearLayout:線性佈局,子元素依橫向或縱向排列。
  • RelativeLayout:相對佈局,以父容器或兄弟元素為基準定位。
  • FrameLayout:框架佈局,所有子元素疊在左上角。
  • TableLayout:表格佈局,以列與欄控制位置。

一、LinearLayout

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="vertical">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="垂直1" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="垂直2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平1" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:orientation="horizontal">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="水平上對齊" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="水平垂直居中" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="水平下對齊" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:hint="請輸入..."/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="提交" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="請輸入..."/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交" />
    </LinearLayout>
</LinearLayout>

orientation:horizontal 或 vertical,決定排列方向。

layout_gravity:元素相對於父容器的對齊方式,可為 top、bottom、center、left、right 等,多值以 | 串接,例如右上對齊寫成 top|right。

layout_weight:佔用剩餘空間的比例。若 A 設 1、B 設 3,兩者分別佔四分之一與四分之三,此時寬度不該再由 layout_width 決定,規範寫法是搭配 0dp。若只有 A 設了 weight、B 沒設且寬度為 wrap_content,則 B 依自身內容佔寬,剩下的空間全給 A。

二、RelativeLayout

<LinearLayout ...>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true" android:text="我在左下"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_centerInParent="true" android:text="我在中間"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" android:text="我在右上"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" android:text="參照按鈕"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_above="@id/button_2"
            android:layout_toRightOf="@id/button_2" android:text="我在右上"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_below="@id/button_2"
            android:layout_toLeftOf="@id/button_2" android:text="我在左下"/>
    </RelativeLayout>
</LinearLayout>

屬性值為 true/false 的對齊類:

layout_centerHorizontal、layout_centerVertical(水平/垂直居中)、layout_centerInParent(相對父元素雙軸居中)、layout_alignParentBottom、layout_alignParentLeft、layout_alignParentRight、layout_alignParentTop。

屬性值為控件 id 的相對定位類:

layout_above、layout_below(在某元素上/下方)、layout_toLeftOf、layout_toRightOf(在某元素左/右方)、layout_alignTop、layout_alignBottom、layout_alignLeft、layout_alignRight(與某元素邊界對齊)。

使用 RelativeLayout 時務必注意:被參照的控件必須先出現在版面檔中,且參照用 @id/ 而非 @+id/,否則會產生非預期的效果。層數越深效能越差,能扁平化就別巢狀。

三、FrameLayout

所有子元素都從左上角開始繪製,後者疊在前者之上,常用於圖片與文字疊放、或 Fragment 容器:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是一個按鈕"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是一個輸入框"/>
</FrameLayout>

四、TableLayout

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
        <TextView android:layout_height="wrap_content" android:text="郵箱"/>
        <EditText android:layout_height="wrap_content"
            android:inputType="textEmailAddress" android:hint="請輸入您的郵箱" />
    </TableRow>
    <TableRow>
        <TextView android:layout_height="wrap_content" android:text="密碼"/>
        <EditText android:layout_height="wrap_content"
            android:inputType="textPassword" android:hint="請輸入密碼" />
    </TableRow>
    <TableRow>
        <Button android:layout_height="wrap_content"
            android:layout_span="2" android:text="注冊" />
    </TableRow>
</TableLayout>

需要讓某一欄自動填滿剩餘寬度時,加上 stretchColumns:

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">
    ...
</TableLayout>

TableRow 代表一行,row 內每個子元素即為一格。

layout_span 用於合併儲存格,設為 2 表示佔兩欄。

stretchColumns 指定要拉伸的欄索引(從 0 起算),設為 1 即讓第二欄吃滿剩餘寬度。

五、自訂佈局的引進

佈局本身可以嵌套佈局。當某段畫面(例如標題欄)獨立性高又重複使用,或版面複雜到難以維護時,把它獨立成一個 XML 再用 include 引入會清楚很多。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/example"/>
</LinearLayout>

選型上可以記一個原則:單純直排或橫排用 LinearLayout 搭配 weight;需要跨列對齊用 RelativeLayout 或 ConstraintLayout;只是疊放圖文用 FrameLayout;表單類以列為單位對齊用 TableLayout。若是新專案,ConstraintLayout 幾乎可以取代前三種,並把巢狀層數壓到最淺,效能表現明顯更好。

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