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

Android 五大佈局詳解:相對、絕對、線性、表格與幀佈局的取舍

編輯:關於 Android 程式設計

寫過網頁的人對「佈局」這個概念不陌生——把頁面切分成區塊,例如 div、table。Android 也是同樣的道理,只是工具換成了五種 Layout。選對佈局,介面會更漂亮,開發也更省力;選錯了,後面全是補救。

要補充的是,Android 不只跑在手機上,平板、電視、智慧家居等設備同樣大量採用,因此自適應的重要性不言而喻。

一、五大佈局分類

  1. 相對佈局 RelativeLayout
  2. 絕對佈局 AbsoluteLayout
  3. 線性佈局 LinearLayout
  4. 表格佈局 TableLayout
  5. 幀佈局 FrameLayout

以下用一個簡單的撥號器介面做示範。

二、逐一動手實作

1. 相對佈局(RelativeLayout)

建立專案時,activity_main.xml 的預設佈局就是 RelativeLayout。它的核心概念是「依子元素之間的相對位置來排列」——父容器可以理解成整個螢幕,子元素則是按鈕、文字框這些零件。

這是 Android 最常用、也最強大的佈局:可設定的屬性極多、能表達的結構最豐富;由於各家螢幕解析度差異大,用它來做自適應最合適;而且相對於絕對座標,以「誰的下面、誰的右邊」來定位直觀得多。

a、XML 實作

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.fivelayout.MainActivity" > <!-- 默認RelativeLayout相對布局 --> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入電話號碼:" /> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入電話號碼" android:layout_below="@id/tv_number" /> <Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="call" android:layout_below="@id/et_number" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="call" android:layout_below="@id/et_number" android:layout_toRightOf="@id/btn_call" /> </RelativeLayout> 

b、常用屬性

  • TextView:顯示文字
  • EditText:輸入框
  • android:id:為元素指定唯一標識
  • android:text:要顯示的文字內容
  • android:layout_below:位於指定子元素下方
  • android:layout_toRightOf:位於指定子元素右方
  • android:layout_width:橫向填充方式
  • android:layout_height:縱向填充方式

c、效果

2. 絕對佈局(AbsoluteLayout)

打個比方:鬥地主時三個玩家的位置固定在各自角落,用相對佈局描述起來很彆扭,而用絕對佈局只要給定座標就好。

但在實際開發中,這種佈局幾乎不被採用:寫法過於剛性,難以適配各種螢幕尺寸,因此 Google 的 Android 團隊已經放棄了它。

它的運作方式是透過 android:layout_x 與 android:layout_y 描述子元素的座標位置。螢幕左上角是原點 (0,0):第一個值為橫座標,向右遞增;第二個值為縱座標,向下遞增。此佈局中的子元素允許互相重疊。

a、XML 實作

 <?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" > <!-- 絕對布局AbsoluteLayout --> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="22dp" android:layout_y="33dp" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="141dp" android:layout_y="103dp" android:text="Button" /> </AbsoluteLayout> 

b、常用屬性

  • android:layout_x:距螢幕左上角橫向的距離
  • android:layout_y:距螢幕左上角縱向的距離

c、效果

3. 線性佈局(LinearLayout)

線性佈局就像串肉串,所有子元素被排在一條線上,可分為水平與垂直兩個方向。

  1. 垂直排列時形成 N 行單列,每行只有一個元素,不論它的寬度多少。
  2. 水平排列時形成單行 N 列。
  3. 想做出兩行兩列的結構,常見做法是:先垂直放兩個元素,每個元素內部再放一個水平方向的 LinearLayout。

也就是說,縱向與橫向完全可以嵌套使用, table 般的排列效果就是這樣做出來的。

a、XML 實作

 <?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" > <!-- 線性布局LinearLayout --> <TextView android:id="@+id/tv_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:textSize="18sp" android:text="請輸入電話號碼:" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="請輸入電話號碼" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="撥打" /> </LinearLayout> 

b、常用屬性

  • android:layout_marginLeft:元素外部距螢幕左側的邊距
  • android:layout_marginTop:元素外部距螢幕上方的邊距
  • android:textSize:字體大小,注意單位是 sp

c、效果

4. 表格佈局(TableLayout)

不管是 Excel 還是 HTML 的 table 標籤,大家對表格都不陌生,Android 的 TableLayout 概念相同。需要多行多列的排列時,它就是首選。

一個 TableLayout 由多個 TableRow 組成,每個 TableRow 代表表格中的一行。

  1. TableRow 是 LinearLayout 的子類。TableLayout 不需要事先宣告幾行幾列,而是由 TableRow 與其中包含的元件數量來決定。
  2. TableRow 相當於 table 裡的 tr,本身是容器。每往 TableRow 裡多放一個元件,表格就多出一列;直接把元件放進 TableLayout,該元件會獨佔一行。
  3. 列的寬度由該列最寬的儲存格決定;整個表格的寬度則取決於父容器,預設填滿父容器。

a、XML 實作

 <?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" > <!-- 表格布局tablelayout --> <!-- tablerow代表一行,行裡面有多少個標簽內容就代表多少列 --> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1行1列" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1行2列" android:textSize="18sp" android:layout_marginLeft="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1行3列" android:textSize="18sp" android:layout_marginLeft="20dp" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2行1列" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2行2列" android:textSize="18sp" android:layout_marginLeft="20dp" /> </TableRow> </TableLayout>

b、常用屬性

TableRow:代表表格中的一行。

c、效果

5. 幀佈局(FrameLayout)

幀佈局有的書也稱框架佈局,是最簡單的一種形式,實戰中使用頻率較低。放進這個佈局的所有子視圖都以層疊方式呈現:第一個加入的元件在最底層,最後加入的在最頂層,上層會覆蓋下層,有點像堆疊(Stack)的效果。

a、XML 實作

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 幀布局FrameLayout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="幀布局..." /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="點擊" /> </FrameLayout> 

b、常用屬性

這個佈局沒有太多需要特別說明的屬性,實作時把 layout_gravity 用好,就能靈活控制子元素在層疊後的落點。

c、效果

上面的效果圖若一時看不出差別,強烈建議自己動手改一改屬性再跑一次,差異會非常明顯。

三、總結

  1. 實戰中這幾種佈局從來不是單打獨鬥,而是層層嵌套,就像 HTML 裡 div 套 table、table 再套 div。
  2. 選哪一種,取決於哪個寫法能更快更清楚地完成畫面,同時也更好維護。
  3. 絕對佈局雖已被官方放棄,但理解它的座標概念仍有幫助。
  4. 好的佈局不只是為了跑起來,也直接決定頁面的視覺品質。
  5. 部分佈局之間可以互相替換,例如相對佈局、線性佈局與表格佈局在特定結構下往往能彼此取代。
  6. 還有大量佈局屬性等著你去挖掘。聰明的你大概已經發現規律了——是不是很像 CSS?不妨順著這個熟悉感繼續深入。

以上就是 Android 五大佈局的完整梳理,希望對你的學習有所幫助。

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