前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LinearLayout.onMesaure-计算LinearLayout的高度

LinearLayout.onMesaure-计算LinearLayout的高度

作者头像
None_Ling
发布2018-10-24 15:11:12
6430
发布2018-10-24 15:11:12
举报
文章被收录于专栏:Android相关Android相关
代码语言:javascript
复制
  if (useLargestChild &&
            (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED)) {
        mTotalLength = 0;

        for (int i = 0; i < count; ++i) {
            final View child = getVirtualChildAt(i);

            if (child == null) {
                mTotalLength += measureNullChild(i);
                continue;
            }

            if (child.getVisibility() == GONE) {
                i += getChildrenSkipCount(child, i);
                continue;
            }

            final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
                    child.getLayoutParams();
            // Account for negative margins
            final int totalLength = mTotalLength;
            mTotalLength = Math.max(totalLength, totalLength + largestChildHeight +
                    lp.topMargin + lp.bottomMargin + getNextLocationOffset(child));
        }
    } 
// Add in our padding   
 mTotalLength += mPaddingTop + mPaddingBottom;
int heightSize = mTotalLength;
// Check against our minimum height
heightSize = Math.max(heightSize, getSuggestedMinimumHeight());
// Reconcile our calculated size with the heightMeasureSpec
int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0);
heightSize = heightSizeAndState & MEASURED_SIZE_MASK;

以上代码为计算LinearLayout总高度的代码

  1. 判断useLargestChild,如果标识位为true的话,说明这是使用最大的子View的高度来作为自己的高度,从判断可以看出,只有当heightMode不是MeasureSpec.EXACTLY的时候,才会走这个判断,意味着,如果不是EXACTLY的话,那么LinearLayout就是可变的了
  2. 接着就将mTotalLength置为0,会遍历所有的子View将最大子View的高度赋给mTotalLength变量,也就是用最大高度的子View来做自己的高度
  3. 将子View的高度再加上上下的padding,获得所需要的总高度
  4. 判断background中Drawable的高度和所需总高度比,拿最大的那个做为所需要的总高度
  5. 通过resolveSizeAndState来获取LinearLayout的高度以及状态
  6. 通过位运算获取高度
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.05.22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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