// mBaselineChildTop 表示指定的 baseline 的子视图的顶部高度 if ((baselineChildIndex >= 0) && (baselineChildIndex == i + 1)) { mBaselineChildTop = mTotalLength; }
// 设置为 baseline 的子视图的前面不允许设置 weiget 属性 if (i < baselineChildIndex && lp.weight > 0) { thrownewRuntimeException("A child of LinearLayout with index " + "less than mBaselineAlignedChildIndex has weight > 0, which " + "won't work. Either remove the weight, or don't set " + "mBaselineAlignedChildIndex."); }
// Either expand children with weight to take up available space or // shrink them if they extend beyond our current bounds. If we skipped // measurement on any children, we need to measure them now.
floatchildExtra= lp.weight; if (childExtra > 0) { // Child said it could absorb extra space -- give him his share //计算weight属性分配的大小 intshare= (int) (childExtra * delta / weightSum); //权重和减去已经分配权重 weightSum -= childExtra; //剩余高度减去分配的高度 delta -= share;
// Child may now not fit in vertical dimension. childState = combineMeasuredStates(childState, child.getMeasuredState() & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT)); }
// Add in our padding mTotalLength += mPaddingTop + mPaddingBottom; // TODO: Should we recompute the heightSpec based on the new total length? } else { alternativeMaxWidth = Math.max(alternativeMaxWidth, weightedMaxWidth);
// We have no limit, so make all weighted views as tall as the largest child. // Children will have already been measured once. if (useLargestChild && heightMode != MeasureSpec.EXACTLY) { for (inti=0; i < count; i++) { finalViewchild= getVirtualChildAt(i);
/** * Utility to reconcile a desired size and state, with constraints imposed * by a MeasureSpec. Will take the desired size, unless a different size * is imposed by the constraints. The returned value is a compound integer, * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the * resulting size is smaller than the size the view wants to be. * * @param size How big the view wants to be. * @param measureSpec Constraints imposed by the parent. * @param childMeasuredState Size information bit mask for the view's * children. * @return Size information bit mask as defined by * {@link #MEASURED_SIZE_MASK} and * {@link #MEASURED_STATE_TOO_SMALL}. */ publicstaticintresolveSizeAndState(int size, int measureSpec, int childMeasuredState) { finalintspecMode= MeasureSpec.getMode(measureSpec); finalintspecSize= MeasureSpec.getSize(measureSpec); finalint result; switch (specMode) { case MeasureSpec.AT_MOST: if (specSize < size) { result = specSize | MEASURED_STATE_TOO_SMALL; } else { result = size; } break; case MeasureSpec.EXACTLY: result = specSize; break; case MeasureSpec.UNSPECIFIED: default: result = size; } return result | (childMeasuredState & MEASURED_STATE_MASK); }
protectedvoidonLayout(boolean changed, int l, int t, int r, int b) { if (mOrientation == VERTICAL) { layoutVertical(l, t, r, b); } else { layoutHorizontal(l, t, r, b); } }
/** * Position the children during a layout pass if the orientation of this * LinearLayout is set to {@link #VERTICAL}. * * @see #getOrientation() * @see #setOrientation(int) * @see #onLayout(boolean, int, int, int, int) * @param left * @param top * @param right * @param bottom */ voidlayoutVertical(int left, int top, int right, int bottom) { finalintpaddingLeft= mPaddingLeft;
int childTop; int childLeft;
//父View默认子View的宽度 finalintwidth= right - left; //子View的右侧默认位置 intchildRight= width - mPaddingRight;