編輯:關於 Android 程式設計
理解版面配置是寫出好 Android 程式的基礎功課。RelativeLayout 是其中功能最強、也最容易被誤用的一個——用對了,它能以極淺的層級搭出複雜畫面;用錯了,只會掉進循環依賴的泥沼。
除了把元件排成一列或一欄的 LinearLayout,RelativeLayout 是另一種極普及的版面型別。與其他 Layout 一樣,它可以透過 XML 資源定義,也可以用 Java 程式建立。
它的行為如其名:相對於其他元件或父容器本身來排列元件。具體來說,子元件(ImageView、TextView、Button 等)可以被放在另一個元件的上方、下方、左方或右方;也可以相對於父容器擺放,包括靠齊四邊緣,以及水平或垂直居中。
子元件的位置由「規則」定義,這些規則決定了元件在 RelativeLayout 中的呈現方式。完整清單可查 RelativeLayout 類別的 SDK 文件,對應的 XML 屬性也在其中。
一個絕對必要的條件:每個子元件都必須正確設定自己的 id,規則才有依據可循。
假設要設計一個畫面,包含一個 EditText 與一個 Button,希望 Button 顯示在 EditText 的右側。做法是定義一個 RelativeLayout,底下兩個子元件各自帶規則:EditText 對齊父容器左側、並位於 Button 左邊;Button 則對齊父容器右側。
XML 寫法如下(放在 res/layout/relative.xml):
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<EditText
android:id="@+id/EditText01"
android:hint="Enter some text..."
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/Button01"
android:text="Press Here!"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
在 Activity 的 onCreate() 中,只要一行就能載入:
setContentView(R.layout.relative);
這個版面設定了寬高皆填滿螢幕,子元件共套用三條規則:EditText01 對齊左側、EditText01 位於 Button01 左邊、Button01 對齊右側。
相對位置也能完全用 Java 描述,靠的是 RelativeLayout.LayoutParams 與 addRule():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText ed = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.LEFT_OF, 1001);
ed.setLayoutParams(params);
ed.setHint("Enter some text....");
Button but1 = new Button(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
but1.setLayoutParams(params2);
but1.setText("Press Here!");
but1.setId(1001);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(ed);
layout1.addView(but1);
setContentView(layout1);
}
這裡有個順序陷阱值得留意:Button 必須在 EditText 引用它之前先設定好 id。程式碼中我把 but1.setId(1001) 放在設定 ed 的規則之後才執行,嚴格來說是有風險的寫法——實務上應該先給 Button 配置 id,再建立對它的引用規則,否則規則會指向一個尚無 id 的元件。
如你所見,元件一多,程式碼量膨脹得很快。因此用程式碼建立版面,最好只用於特殊情況,一般開發仍以 XML 為主。
專屬於 RelativeLayout 的子規則大致分四類:
此外,通用的 ViewGroup 風格屬性也適用,包括必須指定的 layout_height 與 layout_width,以及 margin_top、margin_left、margin_right、margin_bottom 等邊距設定。
來做一個稍複雜的練習。目標畫面用七個不同顏色的 TextView 拼出特定構圖,我們從最終效果反推規則。
步驟一:定義 RelativeLayout 因為它要掌控整個畫面,寬高都設為 fill_parent。
步驟二:放進七個 TextView 各自設定文字、背景色、字色、內距等外觀屬性。
步驟三:為每個元件定義規則
對應的 XML 片段(節錄關鍵規則):
<TextView android:id="@+id/TextView01" android:text="RED"
android:background="#f00" android:textColor="#000"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:padding="25dp" />
<TextView android:id="@+id/TextView02" android:text="ORANGE"
android:layout_centerHorizontal="true"
android:background="#ffa500" />
<TextView android:id="@+id/TextView03" android:text="YELLOW"
android:layout_alignParentRight="true"
android:background="#ffff00" />
<TextView android:id="@+id/TextView04" android:text="GREEN"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/TextView05"
android:background="#0f0" />
<TextView android:id="@+id/TextView05" android:text="BLUE"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:background="#00f" android:textColor="#fff" />
<TextView android:id="@+id/TextView06" android:text="INDIGO"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/TextView05"
android:background="#4b0082" android:textColor="#fff" />
<TextView android:id="@+id/TextView07" android:text="VIOLET"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:background="#ee82ee" />
這個範例示範了 RelativeLayout 最典型的用法:以一個居中的核心元件為錨點,讓其餘元件相對於它展開。這比用巢狀 LinearLayout 一層層包要清爽得多。
id 必須唯一。規則要能正確套用,每個被引用的子元件都得有明確且唯一的 id。
小心循環依賴。當兩個元件互相指向對方時就會發生,例如 A 位於 B 左邊、同時 B 又位於 A 左邊。系統會直接拋出:
IllegalStateException: Circular dependencies cannot exist in a RelativeLayout
值得一提的是,RelativeLayout 的規則是在單一處理階段中解析的,這正是循環無法被容忍的原因。
保持規則精簡。規則越少,循環的機率越低,版面也越好維護、越有彈性。
橫直向都要測。-relative 本質上是比例導向的,同一份版面在橫屏、不同尺寸與解析度下的表現可能落差極大。上機實測無法省略。
用它取代巢狀 LinearLayout。這是最有價值的一條。多層嵌套的 LinearLayout 會讓 measure 階段重複計算,直接影響效能與回應速度。改以 RelativeLayout 扁平化,通常能明顯改善。
RelativeLayout 允許子元件相對於彼此、也相對於父容器的邊緣與中心來組織。掌握規則之後,你能用相當淺的層級搭出複雜構圖,而不必過度嵌套——這既讓版面更易讀,也實際改善了效能。它是每個 Android 開發者都值得花時間弄熟的版面型別。
用 ViewPager 實作底部圓點導航與 Fragment 頁面切換
前一篇我們用 ViewPager 搭配 Fragment 做出了頂部滑塊跟手的滑動效果。本篇換個方向,實作底部圓點指示器,並示範滑動頁面內的控件如何監聽事件,以及 Fra
Android 以 XML 打造動畫介面的完整解析
以 XML 建立動畫的基本流程在 Android 中,若希望介面具有淡入、淡出、縮放或旋轉等效果,可先在 res 下手動建立 anim 資料夾,將動畫定義寫成 XML 檔
刷機精靈下載的 ROM 能卡刷嗎?線刷包與卡刷包的區別
安卓的刷機包分兩種:線刷包與卡刷包。副檔名是 .rom 的通常是線刷包,卡刷包則是 .zip 壓縮檔——格式本身就不同,不能混用。 所以要直接回答這個問題:電腦版刷機精靈
Android 學習之路:數據的使用(二)SQLite 篇
下午學習了 SQLite,並用它做了一個非常簡單的信息管理系統。系統完整做出來之後,我感覺單機版的各類管理系統應該都能拿下來了!下面就開始講講 SQLite 的用法(增刪