LinearLayout.onMeasure-Weight属性的转换

// Either expand children with weight to take up available space or
    // shrink them if they extend beyond our current bounds
    int delta = heightSize - mTotalLength;
    if (delta != 0 && totalWeight > 0.0f) {
        float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;

        mTotalLength = 0;

        for (int i = 0; i < count; ++i) {
            final View child = getVirtualChildAt(i);
            
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
            
            float childExtra = lp.weight;
            if (childExtra > 0) {
                // Child said it could absorb extra space -- give him his share
                int share = (int) (childExtra * delta / weightSum);
                weightSum -= childExtra;
                delta -= share;

                final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                        mPaddingLeft + mPaddingRight +
                                lp.leftMargin + lp.rightMargin, lp.width);

                // TODO: Use a field like lp.isMeasured to figure out if this
                // child has been previously measured
                if ((lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)) {
                    // child was measured once already above...
                    // base new measurement on stored values
                    int childHeight = child.getMeasuredHeight() + share;
                    if (childHeight < 0) {
                        childHeight = 0;
                    }
                    
                    child.measure(childWidthMeasureSpec,
                            MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
                } else {
                    // child was skipped in the loop above.
                    // Measure for this first time here      
                    child.measure(childWidthMeasureSpec,
                            MeasureSpec.makeMeasureSpec(share > 0 ? share : 0,
                                    MeasureSpec.EXACTLY));
                }

                // Child may now not fit in vertical dimension.
                childState = combineMeasuredStates(childState, child.getMeasuredState()
                        & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
            }

            final int margin =  lp.leftMargin + lp.rightMargin;
            final int measuredWidth = child.getMeasuredWidth() + margin;
            maxWidth = Math.max(maxWidth, measuredWidth);

            boolean matchWidthLocally = widthMode != MeasureSpec.EXACTLY &&
                    lp.width == LayoutParams.MATCH_PARENT;

            alternativeMaxWidth = Math.max(alternativeMaxWidth,
                    matchWidthLocally ? margin : measuredWidth);

            allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT;

            final int totalLength = mTotalLength;
            mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredHeight() +
                    lp.topMargin + lp.bottomMargin + getNextLocationOffset(child));
        }

        // Add in our padding
        mTotalLength += mPaddingTop + mPaddingBottom;
        // TODO: Should we recompute the heightSpec based on the new total length?
  1. 通过heightSize-mTotalLength得到delta,也就是还剩余的高度差,它有可能是负数
  2. 判断delta不为0并且totalWeight大于0,那么才开始进行多余空间的分配
  3. 判断mWeightSum是否大于0,这个属性是从外部设置的,如果没有设置的话,就会用自己算出来的totalWeight来作为总weight
  4. 开始遍历所有的子View,并且将空View或者Visible为GONE的子View排除
  5. 从子View的LayoutParams中获取lp.weight属性
  6. 通过计算share,来获取子View可以获得多少的剩余空间
  7. 通过getChildMeasureSpec获取子View的widthMeasureSpec
  8. 将上次measure出的子View高度再加上share的高度获取子View的新高度,再调用child.measure重新计算子View的新高度
  9. 通过child.getMeasuredWidth+margin获取最大的宽度
  10. 判断widthMode不为MeasureSpec.EXACTLY,并且lp.width为LayoutParams.MATCH_PARENT,那么alrtnativeMaxWidth就是margin
  11. 将mTotalLength再加上子View的高度,算出总共的高度

你可能感兴趣的:(LinearLayout.onMeasure-Weight属性的转换)