Android -容器- LinearLayout

  • 目录
    Android -容器- FrameLayout
    Android -容器- LinearLayout
    Android -容器- RelativeLayout

onMeasure 流程

分VERTICAL和HORIZONTAL两种情况,以VERTICAL为例说明,即measureVertical方法。【在有weight的情况也需要测量两次】
mTotalLength:记录子view的总高度
totalWeight:记录权重总和

线性布局可以设置android:divider分割线。
android:measureWithLargestChild="true",所有带权重的子元素都会具有最大子元素的尺寸,且只有当父view布局方向上的宽度或高度为wrap_content才有效。

第一次measure:

// See how tall everyone is. Also remember max width.
// 宽度为最大子view的宽度
for (int i = 0; i < count; ++i) {
  ...
  final boolean useExcessSpace = lp.height == 0 && lp.weight > 0;
  if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) {
  //当前的LinearLayout是EXACTLY模式,且子view的高度为0,且权重大于0, 
  //这个子view只有在LinearLayout高度有剩余的时候,才会根据权重的
  //占比去平 分剩余空间,这是二次测量的这部分。
  skippedMeasure = true;
  //Optimization: don't bother measuring children who are only
  // laid out using excess space. These views will get measured
  // later if we have space to distribute.
  } else {
  //如果当前的LinearLayout不是EXACTLY模式,且子View的weight大于0,
  //优先会把当前LinearLayout的全部可用高度用于子View测量
  final int usedHeight = totalWeight == 0 ? mTotalLength : 0;

    measureChildBeforeLayout(child, i, widthMeasureSpec, 0,
             heightMeasureSpec, usedHeight); //会调用到child.measure()
  }
}

第二次measure:【只有在子view设置了layout_weight时才会进行】

int remainingExcess = heightSize - mTotalLength
    + (mAllowInconsistentMeasurement ? 0 : consumedExcessSpace);

if (skippedMeasure || remainingExcess != 0 && totalWeight > 0.0f) {
  float remainingWeightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;
  mTotalLength = 0;

  for (int i = 0; i < count; ++i) {
    final View child = getVirtualChildAt(i);
    if (child == null || child.getVisibility() == View.GONE) {
      continue;
    }

    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final float childWeight = lp.weight;
    if (childWeight > 0) {
      //权重的分配是一个一个分的,而不是一次分配完成的,如weight分别为:1,2,1。
      //则第一个view为剩余的1/4;第二个view为剩余的1/3;第三个view为剩余的全部。
      final int share = (int) (childWeight * remainingExcess / remainingWeightSum);
      remainingExcess -= share;
      remainingWeightSum -= childWeight;

      final int childHeight;
      if (mUseLargestChild && heightMode != MeasureSpec.EXACTLY) {
        childHeight = largestChildHeight;
      } else if (lp.height == 0 && (!mAllowInconsistentMeasurement
          || heightMode == MeasureSpec.EXACTLY)) {
        // This child needs to be laid out from scratch using
        // only its share of excess space.
        childHeight = share;
      } else {
        // This child had some intrinsic height to which we
        // need to add its share of excess space.
        childHeight = child.getMeasuredHeight() + share;
      }
      final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                            Math.max(0, childHeight), MeasureSpec.EXACTLY);
      final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                            mPaddingLeft + mPaddingRight + lp.leftMargin + 
                            lp.rightMargin, lp.width);
      child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
      ...
    }
  }
}

如果是EXACTLY模式下的weight不为0,且高度设置为0的子View优先级是最低的。如果LinearLayout剩余空间不足,就会不显示 。【尽管该子view排序在前】

onLayout 流程

也分VERTICAL和HORIZONTAL两种情况,以VERTICAL为例子。

  1. 首先根据LinearLayout设置的android:gravity确定childTop的值。
switch (majorGravity) {
  case Gravity.BOTTOM:
    // mTotalLength contains the padding already
    childTop = mPaddingTop + bottom - top - mTotalLength;
    break;

  // mTotalLength contains the padding already
  case Gravity.CENTER_VERTICAL:
    childTop = mPaddingTop + (bottom - top - mTotalLength) / 2;
    break;

  case Gravity.TOP:
  default:
    childTop = mPaddingTop;
    break;
}
  1. 遍历子view根据android:layout_gravity的设置确定childLeft的值。
for (int i = 0; i < count; i++) {
  final View child = getVirtualChildAt(i);
  if (child == null) {
    childTop += measureNullChild(i);
  } else if (child.getVisibility() != GONE) {
    final int childWidth = child.getMeasuredWidth();
    final int childHeight = child.getMeasuredHeight();

    final LinearLayout.LayoutParams lp =
        (LinearLayout.LayoutParams) child.getLayoutParams();

    int gravity = lp.gravity;
    if (gravity < 0) {
      gravity = minorGravity;
    }
    final int layoutDirection = getLayoutDirection();
    final int absoluteGravity=Gravity.getAbsoluteGravity(gravity,layoutDirection);
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
      case Gravity.CENTER_HORIZONTAL:
        childLeft = paddingLeft + ((childSpace - childWidth) / 2)
            + lp.leftMargin - lp.rightMargin;
        break;

      case Gravity.RIGHT:
        childLeft = childRight - childWidth - lp.rightMargin;
        break;

      case Gravity.LEFT:
      default:
        childLeft = paddingLeft + lp.leftMargin;
        break;
    }

    if (hasDividerBeforeChildAt(i)) {
      childTop += mDividerHeight;
    }

    childTop += lp.topMargin;
    child.layout(childLeft, childTop + getLocationOffset(child),
        childWidth, childHeight);
    //childTop累加
    childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);

    i += getChildrenSkipCount(child, i);
  }
}

你可能感兴趣的:(Android -容器- LinearLayout)