View.java
//注意final修飾,該方法永遠不會被覆蓋,整個布局結構measure方法唯一
publicfinalvoid measure(int widthMeasureSpec, int heightMeasureSpec) {
onMeasure(widthMeasureSpec, heightMeasureSpec);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}
//注意final修飾,該方法永遠不會被覆蓋,整個布局結構layout方法唯一
publicfinalvoid layout(int l, int t, int r,int b) {
boolean changed = setFrame(l, t, r, b);
if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
onLayout(changed, l, t, r, b);
}
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { }空方法
ViewGroup.javaextends View.java
@Override
protected abstract void onLayout(boolean changed, int l, int t, int r, int b);
//測量該ViewGroup所包含的所有布局
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {}
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {}
//我會單講mChildren數組mChildren中的View是如何來的。
public View getChildAt(int index) {return mChildren[index];}
public int getChildCount() {return mChildrenCount; }
RelativeLayout.javaextendsViewGroup.java
//當繼承RelativeLayout布局時,我們應當覆蓋該方法,以實現測量該布局包含的View,//此處的實現并不能測量所有的View
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}
protected void onLayout(boolean changed, int l, int t, int r, int b) {}
private void measureChild(View child, LayoutParams params, int myWidth, int myHeight) {}
//還包含一個重要的內部類,代表RelativeLayout所包含的每一個view大小及位置信息
public static class LayoutParams extends ViewGroup.MarginLayoutParams{
private int mLeft, mTop, mRight, mBottom;
}
下面我要自定義一個布局,定義布局的目的肯定是為了向其內部放置View
CustomGridLayout.javaextends RelativeLayout.java
初學者會問,我們到底需要繼承RelativeLayout類的哪個方法呢!!
拋去一切,我們自己想象,布局控件需要
第一:控件(View)的大小
第二:控件(View)的位置
第三:知道要放置多少個View
通過熟讀文檔,我們應該知道:
onMeasure方法負責測量將要放在CustomGridLayout內部的View的大小。
onLayout方法負責分配尺寸及位置給將要放在CustomGridLayout內部的View。
所以很明顯,需要我們繼承的方法是
1.protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}
功能:測量該布局所包含的所有View的大小(會在框架層循環取得每一個View,然后測量其大小),該方法會被View.java中的measure方法調用。而measure方法會被
2.protected void onLayout(boolean changed, int l, int t, int r, int b) {}
功能:在相應的位置放置相應的View,該方法會被View.java中的layout方法調用,而layout方法會被誰調用呢?
(1)調用requestLayout()方法.該方法內部會執行Object.layout(…)
(2)直接調用Object.layout(…)
(3) 調用addView(View child, ...)時,
調用addView(...)之前一般需要先調用android.view.View.setLayoutParams(LayoutParams params)
最后我簡略分析了一下布局調用的流程調用,如下:
也許有人會問當調用addView時,會和框架層的layout,onLayout,measure, onMeasure等幾個布局方法有什么關系,或者說后者幾個方法是怎么被觸發的,別著急,看見addView(...)的底層實現了嗎?
public void addView(View child, int index, LayoutParams params) {
// addViewInner() will call child.requestLayout() when setting the new LayoutParams
// therefore, we call requestLayout() on ourselves before, so that the child's
//requestwill be blocked at our level
requestLayout();
invalidate();
addViewInner(child, index, params, false);
}
很明顯,addView在底層調用了requestLayout方法,該方法如時序圖所示,會依次觸發我們的onMeasure,onLayout方法。