天天看点

安卓layout源码浅析之——FrameLayout

一直想研究下安卓各种布局控件,FrameLayout是安卓最简单的界面布局,所以就从FrameLayout讲起。

1.属性。frameLayout继承ViewGroup,除了拥有ViewGroup的属性之外,只有一个layout_gravity属性。看它的内部静态类LayoutParams:

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

2.绘制过程。首先,它会遍历所有子view,并且对每个子view进行measure,并记录下子view的最大宽高,作为自身的尺寸。在这个过程中,如果自身是不确定大小的模式,子view又是MATCH_PARENT属性的,就需要为这些子view重新测绘。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        (。。。。。。)

        int maxHeight = 0;//记录最大的宽高

        int maxWidth = 0;
        int childState = 0;

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);//测绘所有子view
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                maxWidth = Math.max(maxWidth,
                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = combineMeasuredStates(childState, child.getMeasuredState());
                if (measureMatchParentChildren) {
                    if (lp.width == LayoutParams.MATCH_PARENT ||
                            lp.height == LayoutParams.MATCH_PARENT) {
                        mMatchParentChildren.add(child);//保存需要重新测绘的子view
                    }
                }
            }
        }

     (。。。。。。)
        if (count > 1) {
            for (int i = 0; i < count; i++) {
               <pre name="code" class="java" style="font-size: 13.3333px;">     (。。。。。。)
           

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);//重新测绘子view } } }

3.layout过程。FrameLayout对每个子view的layout过程是相同的。它遍历所有子view,通过子view的gravity属性进行xy轴偏移量的计算,最后调用child.layout()对子View进行布局。

void layoutChildren(int left, int top, int right, int bottom,
                                  boolean forceLeftGravity) {
        (。。。。。。)

        for (int i = 0; i < count; i++) {
            (。。。。。。)

                switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                    case Gravity.CENTER_HORIZONTAL:
                        childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                        lp.leftMargin - lp.rightMargin;
                        break;
                    case Gravity.RIGHT:
                        if (!forceLeftGravity) {
                            childLeft = parentRight - width - lp.rightMargin;
                            break;
                        }
                    case Gravity.LEFT:
                    default:
                        childLeft = parentLeft + lp.leftMargin;
                }

                switch (verticalGravity) {
                    case Gravity.TOP:
                        childTop = parentTop + lp.topMargin;
                        break;
                    case Gravity.CENTER_VERTICAL:
                        childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                        lp.topMargin - lp.bottomMargin;
                        break;
                    case Gravity.BOTTOM:
                        childTop = parentBottom - height - lp.bottomMargin;
                        break;
                    default:
                        childTop = parentTop + lp.topMargin;
                }

                child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }
        }
    }