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

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

編輯:Android 程式設計入門

界面開發是 Android 開發的重要一環,而布局則是組織界面的骨架。本文將系統介紹 Android 中最基礎的四種布局——LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法與常用屬性,幫助你快速掌握界面排列的核心技巧。

  • LinearLayout(線性布局):子元素沿單一方向線性排列
  • RelativeLayout(相對布局):通過相對定位控制控件位置
  • FrameLayout(幀布局):最簡單的布局,所有控件默認堆疊於左上角
  • TableLayout(表格布局):以行列表格的方式組織控件

1. LinearLayout 線性布局

線性布局通過 orientation 屬性決定排列方向,是最常用的布局之一。以下代碼展示了垂直與水平排列、對齊方式以及權重分配的典型用法:

<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>
  • orientation:horizontal(水平)/ vertical(垂直),決定線性排列方向。
  • layout_width / layout_height:設置元素的寬度與高度。
  • layout_gravity:top/bottom/center/left/right 等,表示元素相對父容器的對齊方式,多種方式可用「|」分隔,如右上對齊寫作 top|right。
  • layout_weight:按比例分配剩餘空間。例如 A 設為 1、B 設為 3,則 A、B 分別佔據 1/4 與 3/4 的空間;此時寬度不再由 layout_width 決定,將其設為 0dp 是較規範的寫法。
  • 若僅 A 設為 1、B 不設並將 layout_width 設為具體值或 wrap_content,則 B 由自身寬度決定,A 佔滿剩餘空間。

2. RelativeLayout 相對布局

相對布局允許控件相對於父容器或其他控件進行定位,靈活性很高:

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

屬性值為 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:左(右)邊界對齊

3. FrameLayout 幀布局

幀布局最為簡單,所有子元素都會堆疊在左上角,後添加的控件會覆蓋先添加的:

<FrameLayout 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>

4. TableLayout 表格布局

表格布局以行(TableRow)和列的形式組織控件,適合呈現表單類界面:

<TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1"> <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>
  • TableRow:代表表格中的一行,行內每個元素即為一列。
  • layout_span:合併單元格,設為 2 表示該元素佔據兩列空間。
  • stretchColumns:由於 TableRow 內無法直接指定列寬,可用此屬性拉伸指定列填滿屏幕,設為 1 表示拉伸第 2 列(索引從 0 開始)。

5. 自定義布局的引入

在 Android 中,布局容器內既可以放置控件,也可以放置子布局。當某塊內容相對獨立且被頻繁使用(例如標題欄),或布局結構較為複雜時,可以考慮抽離成自定義布局再引入,這樣能有效提升可維護性。做法很簡單:

  • 新建一個布局文件,例如 example.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>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved