首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LinearLayout.onMeasure-Weight属性的转换

LinearLayout.onMeasure-Weight属性的转换

作者头像
None_Ling
发布2018-10-24 15:11:09
4740
发布2018-10-24 15:11:09
举报
文章被收录于专栏:Android相关Android相关
// 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的高度,算出总共的高度
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档