編輯:進階開發
布局寫得好不好,往往決定了一個 Android 程式的完成度。本篇要談的 LinearLayout(線性布局)是最基礎、也最常被使用的一種,它能沿著垂直或水平單一軸線排列介面元件;只要用得夠熟,很多變化都能從它出發。
顧名思義,LinearLayout 就是把子元件排成一條線。方向設為 vertical 時,所有子元件由上而下堆在同一欄;設為 horizontal 時,則由左而右排在同一列。它既可以寫在 XML 布局資源裡,也能用 Java 程式碼在執行期動態建立。
下圖是一個包含七個 TextView 的線性布局,方向設為垂直,因此七個元件各佔一列。每個 TextView 的文字本身就是一個顏色名稱,背景色也設成對應的顏色;由於 layout_width 設為 fill_parent,每個元件都會拉伸到螢幕寬度。

想把 UI 設計得既好維護又好調整,XML 布局資源是最合適的途徑。它把大量靜態配置從程式碼裡搬了出來,讓「長什麼樣子」與「怎麼運作」彼此分離,後續修改成本大幅降低。
這類檔案一律放在專案的 /res/layout 目錄下。以下就是前面那個彩虹畫面的完整定義,存檔為 /res/layout/rainbow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:text="RED" android:id="@+id/TextView01"
android:layout_height="wrap_content" android:background="#f00"
android:layout_width="fill_parent" android:layout_weight=".14"
android:gravity="center" android:textColor="#000" />
<TextView android:text="ORANGE" android:id="@+id/TextView02"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".15" android:background="#ffa500"
android:gravity="center" android:textColor="#000" />
<TextView android:text="YELLOW" android:id="@+id/TextView03"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".14" android:background="#ffff00"
android:gravity="center" android:textColor="#000" />
<TextView android:text="GREEN" android:id="@+id/TextView04"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".15" android:background="#0f0"
android:gravity="center" android:textColor="#000" />
<TextView android:text="BLUE" android:id="@+id/TextView05"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".14" android:background="#00f"
android:gravity="center" android:textColor="#fff" />
<TextView android:text="INDIGO" android:id="@+id/TextView06"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".14" android:background="#4b0082"
android:gravity="center" android:textColor="#fff" />
<TextView android:text="VIOLET" android:id="@+id/TextView07"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight=".14" android:background="#ee82ee"
android:gravity="center" android:textColor="#000" />
</LinearLayout>
你可能已經注意到,每個子元件都多了一個 layout_weight 屬性,後面會專門談它。
底下兩張圖分別是這個布局在直式與橫式之下的樣子:


在 Activity 裡要載入這個布局,只需要一行:
setContentView(R.layout.rainbow);
除了 XML,你也可以完全用 Java 把布局生出來,靠的是 LinearLayout(android.widget.LinearLayout)這個類別,子元件的細節設定則在 LinearLayout.LayoutParams 裡。一般性的 ViewGroup.LayoutParams(例如 layout_width、layout_height)與 ViewGroup.MarginLayoutParams(邊距相關)同樣適用。
做法是以 new 出來的 LinearLayout 取代原本的布局資源,把它當成父容器交給 setContentView(),再由它掛上所有子檢視。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.rainbow);
TextView tv1 = new TextView(this);
tv1.setText("FIRST");
tv1.setTextSize(100);
tv1.setGravity(Gravity.CENTER);
TextView tv2 = new TextView(this);
tv2.setTextSize(100);
tv2.setGravity(Gravity.CENTER);
tv2.setText("MIDDLE");
TextView tv3 = new TextView(this);
tv3.setTextSize(100);
tv3.setGravity(Gravity.CENTER);
tv3.setText("LAST");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setGravity(Gravity.CENTER);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
setContentView(ll);
}
直式與橫式的呈現結果如下:


回頭看這段程式碼:先建立並設定三個 TextView,分別指定字級、對齊方式與文字內容;接著建立一個垂直方向的 LinearLayout,讓它填滿整個父容器,並用 gravity 把子元件集中到螢幕中央而不是貼在左上角;然後以 addView 逐一掛上;最後把 LinearLayout 傳進 setContentView()。
可以想見,元件一多,程式碼膨脹的速度非常快。因此動態建立布局通常只適合特殊情境,常態性的靜態畫面還是交給 XML 更省心。
LinearLayout 專屬的屬性有三個:
此外還有通用的 ViewGroup 屬性:layout_width 與 layout_height(必填)、margin_left/top/right/bottom 等邊距參數。
多數屬性看名字就懂,唯獨 layout_weight 值得多說兩句。它與其他屬性最大的差異在於:其他屬性是加在 LinearLayout 本身,而它是加在每個子元件上的。數值通常寫成小數,若所有子元件的權重相加剛好等於 1(也就是 100%),就代表彼此按比例瓜分父容器。
回到彩虹的例子。我們希望七個顏色不管螢幕多大都能等比例填滿,於是七份權重都用 1÷7≈0.143。為了讓總和精準等於 1,最後把五個設為 0.14、兩個設為 0.15——差距極小,但首尾那兩塊還是看得出來稍微大一點。
這招在空間充裕時很好用。可是空間一旦吃緊,權重就可能被其他因素蓋掉,例如檢視被裁切,或 TextView 為了不讓文字換行而自行撐開寬度。把同一份 rainbow.xml 改成水平方向(layout_height 仍設為 fill_parent)之後,這個現象就很明顯了。


照理說權重 0.15 的紅色與紫色應該比 0.14 的其他顏色寬一些,實際上卻不是如此。仔細看紅色那块:因為「Red」這個字比「Orange」短,系統自動做了碰撞調整,寧可讓 RED 多佔一點,也不願意把 Orange 折成兩行。結果其實更好看,但如果這不是你想要的行為,就會變成困擾。
Android 的 UI 靠布局來定義,而 LinearLayout 是最基礎的一環:子元件沿單一軸線排列,或橫或直,再透過 gravity 與 layout_weight 做細部微調。把這兩件事搞懂,複合式布局就只是疊上去而已。
Android 3.0 正面臨「無程式可用」的尷尬
Android 3.0 是谷歌進軍平板市場的利器,但在 iPad 發布會上,由於應用稀少、後勁不足,它被 Steve Jobs 特別拿出來嘲笑了一回。 iPad 發布至今
深入認識Android手機系統:從「機器人」到T-Mobile G1
Android手機系統最關鍵的一個特點,是它的應用框架與GUI庫都用Java實作。用最廣泛被採用的開發語言,等於把入門門檻壓到最低,這也讓它在效能與普及度上比同期其他作業
Android 1.6 Donut 正式發布:CDMA 支援、全域搜尋與 Market 大改版
Donut 1.6 登場 2009 年 9 月 16 日,Google 釋出了 Android SDK 1.6r1,也就是後來廣為人知的 Donut 版本。這次更新對開發
NDK 入門專案實戰(五)
7. 加入 Android 專案中運行 把 apps\myjni\libs 整個資料夾複製到 Android 專案底下: 運行 Android 專案,並用 adb sh