編輯:Android 程式設計入門
佈局檔(layout)決定了畫面的骨架。Android 內建的六大基礎佈局各有適用場景,選對容器往往比寫一堆樣式屬性更省事。
1. 線性佈局(LinearLayout)
2. 相對佈局(RelativeLayout)
3. 表格佈局(TableLayout)
4. 網格佈局(GridLayout)
5. 絕對佈局(AbsoluteLayout)
6. 框架佈局(FrameLayout)


最直覺的容器:子元件依單一方向排列,靠 android:orientation 指定垂直或水平。搭配 layout_weight 可以按比例分配剩餘空間,這是它最實用的特性。
<?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"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:gravity="center_horizontal|center_vertical"
android:text="線性佈局"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="200dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal|center_vertical"
android:background="#0000FF"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#00FF00"
android:text="內容一" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#F7F709"
android:text="內容二" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:text="按一" android:onClick="click" />
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:text="按二" android:onClick="click"/>
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:text="按三" android:onClick="click"/>
<Button android:layout_width="0dp" android:layout_height="wrap_content"
android:layout_weight="1" android:text="按四" android:onClick="click"/>
</LinearLayout>
</LinearLayout>
用 weight 分配寬度時,記得把 layout_width 設為 0dp,而不是 wrap_content,否則各元件會先依內容佔位再瓜分剩餘空間,比例就不準了。
実作一個登入頁:
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal|center_vertical"
android:text="線性佈局登錄頁面"
android:textSize="20sp" />
<LinearLayout
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="賬號:"
android:textSize="15sp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密碼:"
android:textSize="15sp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="15dp"
android:orientation="horizontal" >
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="記住賬號" />
<CheckBox
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="記住密碼" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="15dp"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="click"
android:text="登錄"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15sp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="click"
android:text="注冊"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>






相對佈局允許子元件彼此參照,例如「在 A 的下方」「與 B 底部對齊」「靠父容器右側」。由於能大幅減少嵌套層數,在效能上通常比多層 LinearLayout 更友善。代價是屬性繁多,複雜畫面容易難以維護。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="相對佈局登錄頁面"
android:textSize="20sp" />
<TextView
android:id="@+id/labelAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
android:layout_marginLeft="80px"
android:layout_marginTop="30dp"
android:text="賬號:"
android:textSize="15sp" />
<EditText
android:id="@+id/etAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/labelAccount"
android:layout_marginLeft="130px"
android:layout_marginRight="50px" />
<TextView
android:id="@+id/labelPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etAccount"
android:layout_marginLeft="80px"
android:layout_marginTop="20dp"
android:text="密碼:"
android:textSize="15sp" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/labelPassword"
android:layout_marginLeft="130px"
android:layout_marginRight="50px" />
<CheckBox
android:id="@+id/cbAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etPassword"
android:layout_marginLeft="100px"
android:layout_marginTop="20dp"
android:text="記住賬號" />
<CheckBox
android:id="@+id/cbPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/cbAccount"
android:layout_marginLeft="280px"
android:layout_marginRight="50px"
android:text="記住密碼" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/cbPassword"
android:layout_marginLeft="100px"
android:layout_marginRight="290px"
android:layout_marginTop="25dp"
android:text="登錄"
android:textSize="14sp" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignBottom="@id/btnLogin"
android:layout_marginLeft="290px"
android:layout_marginRight="100px"
android:text="注冊"
android:textSize="14sp" />
</RelativeLayout>
注意所有參照用的 id 都必須先宣告,且不能形成循環依賴,否則會在執行期出錯。

TableLayout 以列(TableRow)為單位組織內容,android:stretchColumns 可指定哪些欄位要拉伸填滿剩餘寬度。適合資料型、需要對齊的畫面。
<?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"
android:padding="5dp"
android:stretchColumns="2" >
<TableRow>
<TextView android:layout_width="50dp" android:layout_height="wrap_content"
android:gravity="center" android:text="序號" android:textSize="20sp" />
<TextView android:layout_width="60dp" android:layout_height="wrap_content"
android:gravity="center" android:text="書名" android:textSize="20sp" />
<TextView android:gravity="center" android:text="內容" android:textSize="20sp" />
<TextView android:layout_width="60dp" android:layout_height="wrap_content"
android:gravity="center" android:text="作者" android:textSize="20sp" />
</TableRow>
<View android:layout_height="1.5dp" android:background="#00FF00" />
<TableRow>
<TextView android:gravity="center" android:text="1" />
<TextView android:gravity="center" android:text="西游記" />
<TextView android:gravity="center" android:text="取經" />
<TextView android:gravity="center" android:text="鄭晨" />
</TableRow>
<View android:layout_height="1.5dp" android:background="#00FF00" />
<TableRow>
<TextView android:gravity="center" android:text="2" />
<TextView android:layout_column="2" android:gravity="center" android:text="孫悟空大鬧天空" />
</TableRow>
<View android:layout_height="1.5dp" android:background="#00FF00" />
<TableRow>
<TextView android:gravity="center" android:text="3" />
<TextView android:layout_column="1" android:gravity="center" android:text="盤龍" />
<TextView android:layout_column="3" android:gravity="center" android:text="鄭晨" />
</TableRow>
<View android:layout_height="1.5dp" android:background="#00FF00" />
</TableLayout>
layout_column 可以讓某個元件直接跳到指定欄位,中間跳過的格子會留空,這在合併儲存格的視覺效果上很好用。



GridLayout 以 rowCount / columnCount 定義網格,layout_columnSpan 可讓元件跨越多欄,很適合計算機按鈕這類規則版面。
<?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"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="計算機"
android:textSize="30sp" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:columnCount="4"
android:rowCount="6" >
<EditText
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_gravity="fill_horizontal"
android:editable="false"
android:focusable="false"
android:gravity="right"
android:text="0"
android:textSize="20sp" />
<Button android:text="AC" android:onClick="acClick"/>
<Button android:text="+/-" />
<Button android:text="%" />
<Button android:text="+" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button android:text="x" android:onClick="anClick" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" android:onClick="bnClick" />
<Button android:text="-" />
<Button android:text="1" android:onClick="onClick" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="+" />
<Button android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal" android:text="0" />
<Button android:text="." />
<Button android:text="=" />
</GridLayout>
</LinearLayout>
Activity 部分(僅示範幾個按鍵的處理方式):
public class GridLayoutActivity extends Activity {
private EditText editText;
private boolean lastClickIsNumber = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
editText = (EditText) findViewById(R.id.tvResult);
}
public void acClick(View v) {
editText.setText("0");
}
public void onClick(View v) {
appendText("1");
}
public void anClick(View v) {
appendText("x");
}
public void bnClick(View v) {
appendText("6");
}
private void appendText(String text) {
if (!lastClickIsNumber) {
editText.setText(text);
} else {
String oldContent = editText.getText().toString().trim();
editText.setText(oldContent + text);
}
lastClickIsNumber = true;
}
}


用 layout_x 與 layout_y 直接指定座標。寫起來直觀,但完全綁死在某種螢幕尺寸上,換一台解析度不同的裝置就會跑位,因此官方早已標記為 deprecated。理解原理即可,不建議用於正式專案。
<?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"
android:padding="5dp">
<TextView android:layout_width="100dp" android:layout_height="100dp"
android:layout_x="100dp" android:layout_y="50dp" android:text="你好嗎?"/>
<TextView android:layout_width="100dp" android:layout_height="100dp"
android:layout_x="50dp" android:layout_y="100dp" android:text="我很好!"/>
<TextView android:layout_width="100dp" android:layout_height="100dp"
android:layout_x="200dp" android:layout_y="100dp" android:text="真的嗎?"/>
<TextView android:layout_width="100dp" android:layout_height="100dp"
android:layout_x="100dp" android:layout_y="200dp" android:text="真的!"/>
</AbsoluteLayout>

FrameLayout 讓所有子元件疊在同一個位置,最上層的會蓋住下面的,常用於浮層、Fragment 容器或自定義畫面的底圖。它的結構最簡單,也因此在效能上最輕。
單向排列用 LinearLayout;需要減少嵌套時用 RelativeLayout(新版專案也可考慮 ConstraintLayout);表格化資料用 TableLayout;規則格子用 GridLayout;層疊遮罩用 FrameLayout;絕對佈局則只當歷史知識。實務上真正影響效能的往往不是選了哪個容器,而是嵌套了幾層——能壓平就壓平。
下一篇接著介紹安卓的基礎控制項。
Android架構解析:從Linux核心到應用框架的五層設計
Android作業系統是一個由多層軟體元件組成的架構。整體大致可分為五個部分:Linux核心、函式庫、Android函式庫、Android執行環境、應用框架與應用層。
Android NDK 入門:Java 與 C/C++ 的橋樑
NDK 是什麼NDK 讓 Android 可以使用 C/C++,適合效能關鍵、需重用既有 C/C++ 庫或想增加逆向難度的情境。開發流程下載並設定 NDK 環境變數。建立
深入透視 Android 消息機制:Handler、Looper 與 MessageQueue
每一個 Android 應用在啟動時都會創建一個線程,它被稱為主線程或 UI 線程,應用的所有操作默認都運行在這個線程上。 然而,當我們要進行數據請求、圖片下載或其他耗時
Android 輸入法框架完整入門
Android 提供可擴充的輸入法框架,讓使用者能選擇觸控鍵盤、語音輸入或其他輸入方式。輸入法在本質上是一個包含 InputMethodService 的應用程式。輸入法