天天看點

Layout布局源碼淺析之——FrameLayout

一直想研究下安卓各種布局控件,FrameLayout是安卓最簡單的界面布局,是以就從FrameLayout講起。

1.屬性。frameLayout繼承ViewGroup,除了擁有ViewGroup的屬性之外,隻有一個layout_gravity屬性。看它的内部靜态類LayoutParams:

1 public static class LayoutParams extends MarginLayoutParams {  
2     public int gravity = -1;//唯一的屬性        

2.繪制過程。首先,它會周遊所有子view,并且對每個子view進行measure,并記錄下子view的最大寬高,作為自身的尺寸。在這個過程中,如果自身是不确定大小的模式,子view又是MATCH_PARENT屬性的,就需要為這些子view重新測繪。

1 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
 2        (。。。。。。)  
 3   
 4        int maxHeight = 0;//記錄最大的寬高  
 5   
 6        int maxWidth = 0;  
 7        int childState = 0;  
 8   
 9        for (int i = 0; i < count; i++) {  
10            final View child = getChildAt(i);  
11            if (mMeasureAllChildren || child.getVisibility() != GONE) {  
12                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);//測繪所有子view  
13                final LayoutParams lp = (LayoutParams) child.getLayoutParams();  
14                maxWidth = Math.max(maxWidth,  
15                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);  
16                maxHeight = Math.max(maxHeight,  
17                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);  
18                childState = combineMeasuredStates(childState, child.getMeasuredState());  
19                if (measureMatchParentChildren) {  
20                    if (lp.width == LayoutParams.MATCH_PARENT ||  
21                            lp.height == LayoutParams.MATCH_PARENT) {  
22                        mMatchParentChildren.add(child);//儲存需要重新測繪的子view  
23                    }  
24                }  
25            }  
26        }  
27   
28     (。。。。。。)  
29        if (count > 1) {  
30            for (int i = 0; i < count; i++) {  
31               <pre name="code" class="java" style="font-size: 13.3333px;">     (。。。。。。)  
32 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);//重新測繪子view } } }      

3.layout過程。FrameLayout對每個子view的layout過程是相同的。它周遊所有子view,通過子view的gravity屬性進行xy軸偏移量的計算,最後調用child.layout()對子View進行布局。

1 void layoutChildren(int left, int top, int right, int bottom,  
 2                                   boolean forceLeftGravity) {  
 3         (。。。。。。)  
 4   
 5         for (int i = 0; i < count; i++) {  
 6             (。。。。。。)  
 7   
 8                 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {  
 9                     case Gravity.CENTER_HORIZONTAL:  
10                         childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +  
11                         lp.leftMargin - lp.rightMargin;  
12                         break;  
13                     case Gravity.RIGHT:  
14                         if (!forceLeftGravity) {  
15                             childLeft = parentRight - width - lp.rightMargin;  
16                             break;  
17                         }  
18                     case Gravity.LEFT:  
19                     default:  
20                         childLeft = parentLeft + lp.leftMargin;  
21                 }  
22   
23                 switch (verticalGravity) {  
24                     case Gravity.TOP:  
25                         childTop = parentTop + lp.topMargin;  
26                         break;  
27                     case Gravity.CENTER_VERTICAL:  
28                         childTop = parentTop + (parentBottom - parentTop - height) / 2 +  
29                         lp.topMargin - lp.bottomMargin;  
30                         break;  
31                     case Gravity.BOTTOM:  
32                         childTop = parentBottom - height - lp.bottomMargin;  
33                         break;  
34                     default:  
35                         childTop = parentTop + lp.topMargin;  
36                 }  
37   
38                 child.layout(childLeft, childTop, childLeft + width, childTop + height);  
39             }  
40         }  
41     }