編輯:關於 Android 程式設計
一個 Android 畫面上往往有許多控制元件,那麼該如何安排它們的位置與排列方式呢?這就必須依靠「容器(Layout)」來存放並排列這些元件,其角色就像 HTML 中的 div 與 table。本文系統性地介紹 Android 的幾種主要布局。
Android 常見的布局有:LinearLayout、RelativeLayout、TableLayout 與 AbsoluteLayout。其中 AbsoluteLayout 需靠指定 x/y 座標定位,缺乏彈性,已不建議使用,因此本文著重介紹前三種。
包含在 LinearLayout 中的元件會依序排成一列或一行,概念上類似 Swing 的 FlowLayout。它的常用屬性包括:
horizontal 還是 vertical,例如 android:orientation="vertical",也可用 setOrientation() 在程式碼中設定。layout_width 與 layout_height 指定,可取具體像素值、wrap_content(依內容實際長度)或 fill_parent(填滿可用空間)。layout_weight 分別設為 2 與 1。以下範例展示一個巢狀的線性布局:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 橫向排列 -->
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView android:text="red" android:background="#aa0000"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="green" android:background="#00aa00"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:text="blue" android:background="#0000aa"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
相對布局靠著「與父容器」或「與同一容器中其他元件」的相對位置來排列,彈性極高。常用屬性分兩類:
相對父容器:
android:layout_alignParentTop:頂部與父容器頂部對齊,另有 alignParentBottom、alignParentLeft、alignParentRight。相對其他元件:
android:layout_above:位於某元件上方,另有 layout_below、layout_toLeftOf、layout_toRightOf。android:layout_alignTop:與某元件頂部對齊,另有 layout_alignBottom、layout_alignLeft、layout_alignRight。既然是「相對」於某元件,就必須指名是哪一個。例如元件 A 的 id 為 @+id/widget_a,若 B 要排在 A 下方,就寫 android:layout_below="@id/widget_a"。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
表格布局類似 HTML 的 Table,以 TableRow 定義每一列。若某元件要跨越多列,可設定 android:layout_span(類似 HTML 的 colspan);預設元件會依序落在每一欄,也可用 android:layout_column 指定欄位。若某欄內容過長或過短,可用 android:stretchColumns 與 android:shrinkColumns 來撐開或縮窄該欄。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:layout_column="1" android:text="Open..."
android:padding="3dip" />
<TextView android:text="Ctrl-O" android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView android:layout_column="1" android:text="Save..."
android:padding="3dip" />
<TextView android:text="Ctrl-S" android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
在上例中透過 android:stretchColumns="1" 把第二欄撐開,讓版面更平均。
實務上:LinearLayout 適合簡單的直線排列;RelativeLayout 彈性最高、能減少不必要的巢狀,適合較複雜的畫面;TableLayout 則專精於表格式的資料呈現。熟悉這三者,幾乎就能應付絕大多數的 Android 畫面配置需求。
Android UI 進階:活用 SlidingDrawer 抽屜控制項
最近在開發一個應用時,有個想法:把設定頁面和應用頁面放在一起,讓使用者能即時看到自己調整設定後對 UI 的影響,這樣操作起來會更方便。琢磨了好一陣子,最後發現 Slidi
Android 安全攻防:反編譯與混淆技術完全解析(下)
上篇介紹了反編譯與重新打包,本文接著說明代碼混淆。混淆並不是阻止反編譯,而是把類名、方法名、變量名改成無意義名稱,增加逆向理解成本。對計算機而言,名稱只是符號;對破解者而
重溫 Android 網絡開發基本功:HttpURLConnection、HttpClient 與 JSON 解析
長期使用各種開源網絡框架之後,反而容易把最底層的東西拋在腦後。偶爾回頭看看原生 API,才發現這些「老傢伙」依然是理解網絡請求的最佳起點。本文就帶大家重新梳理 Andro
Android 啟動優化完整指南:冷熱啟動差異、adb 量化與體驗補強
啟動速度是最直接影響使用者第一印象的指標。本文從啟動模式談到量化方法,再給出可落地的優化策略,最後示範怎麼用主題背景把「等待」變得不那麼難熬。 一、冷啟動與熱啟動 冷