編輯:關於 Android 程式設計
自定義 ViewGroup 時,最容易犯的錯誤就是把邏輯寫在了錯誤的回調裡——比如在構造方法中獲取寬高(此時必然是 0),或者在 onMeasure 裡做本該在 onLayout 做的事。理解生命週期的執行時機與觸發條件,是寫出正確自定義控件的基礎。
ViewGroup 的常用生命週期方法包括:
Context、Context + AttributeSet、Context + AttributeSet + defStyleAttr),代碼中直接 new 時只走第一個,XML 靜態引用時走第二個(API 5 之後的 defStyleAttr 版本由系統按情況調用)。至於 onDraw() 和 dispatchDraw(),分別負責自身背景內容與子 View 的繪製,屬於繪製階段而非生命週期範疇,這裡不展開。
第一種:XML 靜態引用
執行順序為:構造方法 → onFinishInflate()(僅一次)→ onMeasure()(可能多次)→ onSizeChanged()(尺寸變化時)→ onLayout() → onMeasure() → onLayout() …… 後兩個方法會隨每次佈局請求循環往復。
第二種:Activity 中 setContentView(new CustomView(this))
與第一種相比,差別有兩點:構造方法走的是單參數版本;onFinishInflate() 不會被調用(因爲沒有走 XML 填充流程)。其餘順序基本一致。
第三種:直接 new 出來但不掛到任何父佈局上
只會執行構造方法,後續測量與佈局都不會發生。這也是很多初學者疑惑「我的 onMeasure 怎麼不進來」的原因。
onMeasure() 的核心職責是給自己和每個子 View 確定測量規格。第一步永遠是設置自身尺寸:
int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(widthSize, heightSize);
第二步是為子 View 構造測量規格。這裡有兩種典型場景:需要邊距約束的用 getChildMeasureSpec() 讓系統幫你算;要求精確值的直接用 MeasureSpec.makeMeasureSpec()。
// 帶 margin 約束的子 View
View leftMenuView = getChildAt(1);
MarginLayoutParams lp = (MarginLayoutParams) leftMenuView.getLayoutParams();
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
mMinDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
lp.topMargin + lp.bottomMargin, lp.height);
leftMenuView.measure(drawerWidthSpec, drawerHeightSpec);
// 要求填滿剩餘空間的子 View
View contentView = getChildAt(0);
lp = (MarginLayoutParams) contentView.getLayoutParams();
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
contentView.measure(contentWidthSpec, contentHeightSpec);
getChildMeasureSpec(int spec, int padding, int childDimension) 返回的是子 View 的測量規格,padding 參數用於扣除父容器和 margin 的佔用,不需要扣除時就照 contentView 那樣直接構造。
onLayout() 則簡單得多:拿著 onMeasure 階段算好的尺寸,用 layout(left, top, right, bottom) 把每個子 View 擺到正確位置。
這兩個方法經常被混用,但含義不同:
setMeasuredDimension() 後就有值,且不會再變。也就是說 onMeasure() 跑完即可獲取。onLayout() 之後才可靠。如果 onLayout() 沒有修改子 View 的寬高,兩者數值相等;否則會不一致。實踐建議:在 onMeasure 內部只用 getMeasuredWidth/Height,在事件處理等佈局已完成的場景用 getWidth/Height。在 Activity 的 onCreate 裡直接調 getWidth() 拿到 0,就是這個道理。
下面這張圖把上述順序串了起來,可以作為自定義 ViewGroup 開發時的參照:

總結一句:onMeasure 決定「多大」,onLayout 決定「放哪」,onDraw 決定「長什麼樣」。三個階段各司其職、互不越界,自定義控件的行為才會可預期。
解決報錯:You need to use a Theme.AppCompat theme (or descendant) with this activity
一、問題現象 想讓某個 Activity 不顯示標題欄,於是在 AndroidManifest.xml 中給它加上了: android:theme=@android:st
自訂元件三部曲之繪圖篇(十三):Canvas 與圖層(一)
講完 Paint 的幾個方法之後,有必要補一篇 Canvas 的系統性說明。前面已經介紹過 Canvas 的繪圖用法與 save()、restore(),這篇則聚焦在「圖
Android Drawable mutations:為何改一個按鈕透明度會影響另一個?
前言Android 為節省記憶體,可能讓多個控制項共用同一個 Drawable 資源。若只修改其中一個控制項的 alpha,另一個也可能跟著變化。本文說明 Drawabl
Android 實用調試技巧:從 Log API 到 Android Studio 進階斷點
上一篇談的是 UI 優化:include、merge、ViewStub 如何精簡佈局並改善繪製流程、過度繪製的成因與改善方式,以及幾個實用的小 tips。這篇換個主題,談