編輯:Android 開發實例
本篇整理 android.widget.RelativeLayout,對應 Android 2.3 r1 版本。
public class RelativeLayout extends ViewGroup
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.RelativeLayout
直接子類有 DialerFilter 與 TwoLineListItem。
顧名思義,這是相對配置容器:内部的每個子元素,可以用「相對於其他子元素」或「相對於父容器」的方式來定位,而不必像 LinearLayout 那樣一層層嵌套。
但要特別注意:容器與子元素之間不能形成循環依賴。最典型的錯誤就是把 RelativeLayout 的高度設為 WRAP_CONTENT,同時又讓某個子元素 ALIGN_PARENT_BOTTOM——容器高度本身取決於內容,而內容又要對齊容器底邊,邏輯上永遠收斂不了。這道理不難理解,卻是初學者最常踩的坑。官方 SDK 中的 Relative Layout tutorial 有完整的示範,值得先看過再動手。
內部類別 RelativeLayout.LayoutParams 承載與此配置相關的版面資訊。
android:gravity:設定容器內元素的整體定位方式(對象本身的 x/y 軸)。android:ignoreGravity:指定哪個子元素不受容器 gravity 影響。接收子元素的 id,設為 0 則所有子元素都會受到影響。boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event):把無障礙事件分發給子檢視,完成分發回傳 true。RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs):依屬性集合產生新的版面參數。int getBaseline():回傳文字基線到上邊界的偏移量;不支援基線對齊時回傳 -1。void requestLayout():當某個檢視的改變會影響整體配置時呼叫,會觸發整棵視圖樹重新配置。void setGravity(int gravity):描述子檢視該如何擺放,預設為 Gravity.LEFT | Gravity.TOP——也就是說,完全沒設定位置的元素會從左上角開始堆疊。void setHorizontalGravity(int horizontalGravity) / void setVerticalGravity(int verticalGravity):分別設定水平與垂直方向的 gravity。void setIgnoreGravity(int viewId):指定哪個子檢視要忽略容器的 gravity;若容器設為 LEFT | TOP,這個設定不會有任何效果。傳入 0 則沒有任何 View 被排除。boolean checkLayoutParams(ViewGroup.LayoutParams p):檢查傳入的參數是否為合法的 RelativeLayout.LayoutParams。ViewGroup.LayoutParams generateDefaultLayoutParams():回傳寬高皆為 WRAP_CONTENT、座標 (0, 0) 的預設參數。ViewGroup.LayoutParams generateDefaultLayoutParams(ViewGroup.LayoutParams p):當外部傳入的版面參數沒通過 checkLayoutParams() 時呼叫,此方法會轉換成適合本容器的一組參數。void onLayout(boolean changed, int l, int t, int r, int b):為每個子元素分配大小與位置時呼叫,子類可覆寫以重新安排配置。void onMeasure(int widthMeasureSpec, int heightMeasureSpec):量測寬與高。覆寫時必須呼叫 setMeasuredDimension() 保存結果,並確保數值不小於 getSuggestedMinimumWidth() 與 getSuggestedMinimumHeight()。這段配置示範了「標籤 → 輸入框 → 兩顆並排按鈕」的表單排版,全靠相對定位完成,不需要任何嵌套:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@android:drawable/editbox_background"
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:layout_marginLeft="10dip"
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>
值得學的是最後那顆 Cancel 按鈕:layout_toLeftOf 與 layout_alignTop 同時指向 @id/ok,兩個元素便自動維持水平並排與頂端對齊。這正是 RelativeLayout 的價值所在——層數淺、宣告直觀,效能也往往優於多層 LinearLayout 嵌套。
Android 登入畫面實作:密碼遮罩、錯誤提示與三次鎖定
幾乎每個需要使用者帳號的 App,都得面對同一道關卡:登入畫面。它看似簡單,卻同時牽涉到輸入欄位的型別設定、按鈕事件綁定、錯誤訊息回饋,以及最容易被忽略的一環——防止暴力
Android Matrix 圖像變換完全解析:平移、旋轉、鏡像、錯切與復合變換
線性代數裡學過矩陣,但真正把矩陣用來做圖像變換,往往是在實際項目中才想通。這篇把 android.graphics.Matrix 的理論與用法系統梳理一遍。 一、3×3
WebView 使用要點整理:從權限、JS 到返回鍵的完整清單
WebView 基本上就是一個迷你可視瀏覽器,採用 WebKit 核心,因此 HTML、JavaScript、CSS 都能完整支援。但正因為它幾乎是個瀏覽器,新手常會遇到
Android MediaPlayer 完整指南:播放控制、進度條與快轉倒轉
要在 App 裡播放音訊或影片,Android 提供了不少途徑,而 MediaPlayer 是最直覺的一個。它讓你以幾行程式碼就能控制內建媒體播放服務,完成播放、暫停、跳