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总高度的代码
- 判断useLargestChild,如果标识位为true的话,说明这是使用最大的子View的高度来作为自己的高度,从判断可以看出,只有当heightMode不是MeasureSpec.EXACTLY的时候,才会走这个判断,意味着,如果不是EXACTLY的话,那么LinearLayout就是可变的了
- 接着就将mTotalLength置为0,会遍历所有的子View将最大子View的高度赋给mTotalLength变量,也就是用最大高度的子View来做自己的高度
- 将子View的高度再加上上下的padding,获得所需要的总高度
- 判断background中Drawable的高度和所需总高度比,拿最大的那个做为所需要的总高度
- 通过resolveSizeAndState来获取LinearLayout的高度以及状态
- 通过位运算获取高度