专栏首页Android相关LinearLayout.onMeasure-Weight属性的转换

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的高度,算出总共的高度

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Flutter--Dart学习

    2011年10月公开。它的开发团队由Google Chrome浏览器V8引擎 (JavaScript引擎)")团队的领导者拉尔斯·巴克主持,目标在于成为下一代结...

    None_Ling
  • LinearLayout.onMeasure-获取子View总高度

    None_Ling
  • CoordiantorLayout与Behavior

    CoordinatorLayout继承自FrameLayout,并且实现了NestedScrollingParent2接口用于接收嵌套滑动的事件。并且内部定义了...

    None_Ling
  • POJ 3241Object Clustering曼哈顿距离最小生成树

    We have N (N ≤ 10000) objects, and wish to classify them into several groups by ...

    风骨散人Chiam
  • 图论--曼哈顿距离最小生成树模板

    风骨散人Chiam
  • 【最短路算法】Dijkstra+heap和SPFA的区别

    单源最短路问题(SSSP)常用的算法有Dijkstra,Bellman-Ford,这两个算法进行优化,就有了Dijkstra+heap、SPFA(Shortes...

    饶文津
  • IntelliJ IDEA 从入门到上瘾教程,2019图文版!

    如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了。

    Java团长
  • IntelliJ IDEA 2019从入门到上瘾 图文教程

    如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了。

    用户1516716
  • 排序算法之选择排序与冒泡排序

    选择排序与冒泡排序都是比较简单的办法,非常容易理解,但时间复杂度也都是O(N2)。

    dejavu1zz
  • IntelliJ IDEA 真香教程

    如果说IntelliJ IDEA是一款现代化智能开发工具的话,Eclipse则称得上是石器时代的东西了。

    一个优秀的废人

扫码关注云+社区

领取腾讯云代金券