一、布局的起點(diǎn) - performTraversals
和前面分析測量過程類似,整個(gè)布局的起點(diǎn)也是在ViewRootImpl
的performTraversals
當(dāng)中:
private void performTraversals() {
......
mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());
......
}
可以看到,布局過程會參考前面一步測量的結(jié)果,和測量過程的measure
和onMeasure
方法很像,布局過程也有兩個(gè)方法layout
和onLayout
,參考前面的分析,我們先對這兩個(gè)方法進(jìn)行介紹:
1.1 layout
和onLayout
- 對于
View
-
layout
方法是public
的,和measure
不同的是,它不是final
的,也就是說,繼承于View
的控件可以重寫layout
方法,但是我們一般不這么做,因?yàn)樵谒?code>layout方法中又調(diào)用了onLayout
,所以繼承于View
的控件一般是通過重寫onLayout
來實(shí)現(xiàn)一些邏輯。public void layout(int l, int t, int r, int b) { //.... boolean changed = isLayoutModeOptical(mParent) ? setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b); if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) { onLayout(changed, l, t, r, b); } //... }
-
onLayout
方法是一個(gè)空實(shí)現(xiàn)。
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}
- 對于
ViewGroup
- 它重寫了
View
中的layout
,并把它設(shè)為final
,也就是說繼承于ViewGroup
的控件,不能重寫layout
,在layout
方法中,又會調(diào)用super.layout
,也就是View
的layout
。@Override public final void layout(int l, int t, int r, int b) { if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { if (mTransition != null) { mTransition.layoutChange(this); } super.layout(l, t, r, b); } else { // record the fact that we noop'd it; request layout when transition finishes mLayoutCalledWhileSuppressed = true; } }
-
onLayout
方法重寫View
中的onLayout
方法,并把它聲明成了abstract
,也就是說,所有繼承于ViewGroup
的控件,都必須實(shí)現(xiàn)onLayout
方法。
@Override
protected abstract void onLayout(boolean changed, int l, int t, int r, int b);
- 對于繼承于
View
的控件
例如TextView
,我們一般不會重寫layout
,而是在onLayout
中進(jìn)行簡單的處理。 - 對于繼承于
ViewGroup
的控件 - 例如
LinearLayout
,由于ViewGroup
的作用是為了包裹子View
,而每個(gè)控件由于作用不同,布局的方法自然也不同,這也是為了安卓要求每個(gè)繼承于ViewGroup
的控件都必須實(shí)現(xiàn)onLayout
方法的原因。 - 因?yàn)?code>ViewGroup的
layout
方法不可以重寫,因此,當(dāng)我們通過父容器調(diào)用一個(gè)繼承于ViewGroup
的控件的layout
方法時(shí),它最終會回調(diào)到該控件的onLayout
方法。
1.2 布局onLayout(boolean changed, int l, int t, int r, int b)
參數(shù)說明
當(dāng)onLayout
方法被回調(diào)時(shí),傳入了上面這四個(gè)參數(shù),經(jīng)過前面的分析,我們知道onLayout
是通過layout
方法調(diào)用過來,而layout
方法父容器調(diào)用的,父容器在調(diào)用的時(shí)候是根據(jù)自己的坐標(biāo)來計(jì)算出寬高,并把自己的位置的左上角當(dāng)作是(0,0)
點(diǎn),重新決定它所屬子View
的坐標(biāo),因此這個(gè)矩形的四個(gè)坐標(biāo)是相對于父容器的坐標(biāo)值。
1.3 布局的遍歷過程
雖然layout
在某些方面和measure
有所不同,但是它們有一點(diǎn)是共通的,那就是:它們都是作為整個(gè)從根節(jié)點(diǎn)到葉節(jié)點(diǎn)傳遞的紐帶,當(dāng)從父容器到子View
傳遞的過程中,我們不直接調(diào)用onLayout
,而是調(diào)用layout
。
onMeasure
在測量過程中負(fù)責(zé)兩件事:它自己的測量和它的子View
的測量,而onLayout
不同:它并不負(fù)責(zé)自己的布局,這是由它的父容器決定的,它僅僅負(fù)責(zé)自己的下一級子View
的布局。
再回到文章最開始的點(diǎn),起點(diǎn)是通過mView
也就是DecorView
的layout
方法觸發(fā)的,而DecorView
實(shí)際上是一個(gè)FrameLayout
,經(jīng)過前面的分析,我們應(yīng)該直接去看FrameLayout
的onLayout
方法:
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
layoutChildren(left, top, right, bottom, false /* no force left gravity */);
}
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
}
}
可以看到,在它的onLayout
當(dāng)中,又調(diào)用了它的子View
的layout
,那么這時(shí)候就分為兩種情況,一種是該child
是繼承于ViewGroup
的控件并且它有子節(jié)點(diǎn),那么child.layout
方法最終又會調(diào)用到child.onLayout
,在里面,它同樣會進(jìn)行和FrameLayout
所類似的操作,繼續(xù)調(diào)用child
的子節(jié)點(diǎn)的layout
;另一種是該child
是View
或者是繼承于View
的控件或者是它是繼承于ViewGroup
的控件但是沒有子節(jié)點(diǎn),那么到該child
節(jié)點(diǎn)的布局遍歷過程就結(jié)束了。
1.4 小結(jié)
通過分析測量和布局的過程,它們基于一個(gè)思想,把傳遞和實(shí)現(xiàn)這兩個(gè)邏輯分開在不同的函數(shù)中處理,在實(shí)現(xiàn)當(dāng)中,再去決定是否要傳遞。