編輯:關於 Android 程式設計
寫過網頁的人對「佈局」這個概念不陌生——把頁面切分成區塊,例如 div、table。Android 也是同樣的道理,只是工具換成了五種 Layout。選對佈局,介面會更漂亮,開發也更省力;選錯了,後面全是補救。
要補充的是,Android 不只跑在手機上,平板、電視、智慧家居等設備同樣大量採用,因此自適應的重要性不言而喻。
以下用一個簡單的撥號器介面做示範。
建立專案時,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、常用屬性
c、效果

打個比方:鬥地主時三個玩家的位置固定在各自角落,用相對佈局描述起來很彆扭,而用絕對佈局只要給定座標就好。
但在實際開發中,這種佈局幾乎不被採用:寫法過於剛性,難以適配各種螢幕尺寸,因此 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、常用屬性
c、效果

線性佈局就像串肉串,所有子元素被排在一條線上,可分為水平與垂直兩個方向。
也就是說,縱向與橫向完全可以嵌套使用, 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、常用屬性
c、效果

不管是 Excel 還是 HTML 的 table 標籤,大家對表格都不陌生,Android 的 TableLayout 概念相同。需要多行多列的排列時,它就是首選。
一個 TableLayout 由多個 TableRow 組成,每個 TableRow 代表表格中的一行。
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、效果

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

上面的效果圖若一時看不出差別,強烈建議自己動手改一改屬性再跑一次,差異會非常明顯。
以上就是 Android 五大佈局的完整梳理,希望對你的學習有所幫助。
百度地圖 Android 起步:打造一張會呼吸的基本地圖
一切地圖應用的起點,都是一張能顯示、能縮放、能平移的基本地圖。本文示範如何用 TextureMapView 建立最簡單的地圖頁面,並妥善管理它的生命週期,還能監聽地圖載入
Android 官方入門教學(七):為操作欄套用樣式
Styling the Action Bar — 樣式化操作欄 本節教你: 1. 使用 Android 主題 2. 自定義背景 3. 自定義文字顏色 4. 自定義標籤指示
源碼解析 Android Adapter 家族:BaseAdapter、ArrayAdapter、SimpleAdapter 與 SimpleCursorAdapter
Adapter 相當於一個資料源,為 AdapterView 提供資料並據此產生對應的 UI;透過呼叫 AdapterView 的 setAdapter 方法,就能把 A
自定義 FragmentTabHost 實現 360 手機助手同款波紋切換動畫
新版 360 手機助手的底部導航做得很精緻,切換 Tab 時那層從中央蕩開的波紋動畫尤其討喜。前陣子翻到一個開源專案,才發現用 Drawable 做動畫居然這麼順手,於是