LinearLayout 主要是水平或者垂直排列子View淳梦,所以在調(diào)用三大方法時(shí)都是分為水平或者垂直方向的此衅。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mOrientation == VERTICAL) {
measureVertical(widthMeasureSpec, heightMeasureSpec);
} else {
measureHorizontal(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (mOrientation == VERTICAL) {
layoutVertical(l, t, r, b);
} else {
layoutHorizontal(l, t, r, b);
}
}
一般ViewGroup是不會(huì)做繪制操作的金句,LinearLayout因?yàn)橛蟹指罹€ divider 的屬性泪勒,所以會(huì)在 onDraw() 中繪制分割線栅组。
@Override
protected void onDraw(Canvas canvas) {
if (mDivider == null) {
return;
}
if (mOrientation == VERTICAL) {
drawDividersVertical(canvas);
} else {
drawDividersHorizontal(canvas);
}
}
measureVertical(),水平垂直邏輯類似梅掠。
- 通過 mTotalLength 計(jì)算所有childView 占用的高度酌住,(高度為wrap_content時(shí)用于計(jì)算LinearLayout的高度), 最后根據(jù)LinearLayout的高度計(jì)算出剩余空間,用于weight屬性分配剩余空間
- 使用 maxWidth 記錄 childView 最大的寬度阎抒,用于計(jì)算LinearLayout的寬度
- 第一次循環(huán)赂韵,如果childView的屬性 height=0,weight>0挠蛉;LinearLayout 的 heightMode 為固定大小
···
final boolean useExcessSpace = lp.height == 0 && lp.weight > 0;
if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) {
···
則會(huì)跳過childView的第一次measure過程祭示,所以這樣設(shè)置屬性可以提升性能
- 第一次循環(huán),除了3這種情況的childView谴古,所有的childView都會(huì)measure 一次
final int usedHeight = totalWeight == 0 ? mTotalLength : 0;
measureChildBeforeLayout(child, i, widthMeasureSpec, 0,
heightMeasureSpec, usedHeight);
- 第二次循環(huán)质涛,設(shè)置 measureWithLargestChild="true" 屬性 并且高度為 warp_content ,則會(huì)進(jìn)入第二次循環(huán)掰担,用最大 childView 的高度 * childView 個(gè)數(shù) 再次計(jì)算 mTotalLength,防止LinearLayout高度出現(xiàn)問題汇陆。
if (useLargestChild && (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED)) {
mTotalLength = 0;
for (int i = 0; i < count; ++i) {
...
mTotalLength = Math.max(totalLength, totalLength + largestChildHeight + lp.topMargin +
lp.bottomMargin + getNextLocationOffset(child));
...
}
}
根據(jù) mTotalLength 與 LinearLayout的高度,計(jì)算出剩余空間带饱,第三次循環(huán)會(huì)weight >0 的 childView 會(huì)調(diào)用 measure 計(jì)算大小
高度為 wrap_content 時(shí)毡代,因?yàn)闆]有剩余空間,進(jìn)入不到第三次循環(huán)勺疼,所有這個(gè)情況 weight 屬性無效教寂。
android:measureWithLargestChild="true" 屬性需要weight屬性配合使用,不然只會(huì)增加 LinearLayout的高度执庐。
代碼只會(huì)進(jìn)入第二個(gè)循環(huán)酪耕,用最大childView 的大小計(jì)算 LinearLayout的高度,但不會(huì)進(jìn)入第三個(gè)循環(huán)來重新設(shè)置childView的大小轨淌。
float childExtra = lp.weight;
if (childExtra > 0) {
child.measure(MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(),MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(largestChildHeight,MeasureSpec.EXACTLY));
}
最后迂烁,當(dāng)LinearLayout 寬度為wrap_content, childView 高度為 match_parent, LinearLayout 會(huì)最后再次 measure 寬度為match_parent 的childView,所以開發(fā)中要避免這種情況看尼。
當(dāng)LinearLayout 寬度為wrap_content,childView 的寬度盟步,一個(gè)為 wrap_content ,一個(gè)為 match_parent,以wrap_content 為準(zhǔn)
if (!allFillParent && widthMode != MeasureSpec.EXACTLY) {
maxWidth = alternativeMaxWidth; //wrap_content view的寬度
}
- android:measureWithLargestChild="true" 與 divider 屬性沖突藏斩,因?yàn)榈诙窝h(huán)沒有記錄 divider 高度,在展示時(shí)會(huì)出現(xiàn)問題
layoutVertical()却盘,水平垂直邏輯類似狰域。
onLayout() 主要用于確認(rèn)View展示的位置,一般有父容器確認(rèn)谷炸,所以這個(gè)方法主要用于確認(rèn)childView 展示的位置。
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
確認(rèn)位置其實(shí)只要知道左上角位置即可,width,height 在measure 過程已經(jīng)獲得禀挫。
child.layout(left, top, left + width, top + height);
所以主要的邏輯是獲取到 top left旬陡。
- 根據(jù) LinearLayout 的 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;
}
- 根據(jù) childView 的 layout_garvity 屬性獲得 left
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;
}
- 就能確定 childView 的位置了
childTop += lp.topMargin;
setChildFrame(child, childLeft, childTop + getLocationOffset(child),
childWidth, childHeight);
drawDividersVertical(),水平垂直邏輯類似语婴。
void drawDividersVertical(Canvas canvas) {
final int count = getVirtualChildCount();
for (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child != null && child.getVisibility() != GONE) {
//判斷當(dāng)前View之前是否要繪制分割線
if (hasDividerBeforeChildAt(i)) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int top = child.getTop() - lp.topMargin - mDividerHeight;
drawHorizontalDivider(canvas, top);
}
}
}
//判斷是否要繪制最后的分割線
if (hasDividerBeforeChildAt(count)) {
final View child = getLastNonGoneChild();
int bottom = 0;
if (child == null) {
bottom = getHeight() - getPaddingBottom() - mDividerHeight;
} else {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
bottom = child.getBottom() + lp.bottomMargin;
}
drawHorizontalDivider(canvas, bottom);
}
}
所有代碼與注釋,可以通過自定義LinearLayout的方式打斷點(diǎn)調(diào)試
void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
mTotalLength = 0; // 所有 childView 的高度和 + 本身的 padding描孟,注意:它和 LinearLayout 本身的高度是不同的
int maxWidth = 0; // 所有 childView 中寬度的最大值
int childState = 0;
int alternativeMaxWidth = 0; // 所有 layout_weight <= 0 的 childView 中寬度的最大值 用于獲取最大款第
int weightedMaxWidth = 0; // 所有 layout_weight >0 的 childView 中寬度的最大值 用于獲取最大款第
boolean allFillParent = true; //是否 layout_width 全是 matchParent
float totalWeight = 0; // 所有 childView 的 weight 之和
final int count = getVirtualChildCount(); //getCount()
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
boolean matchWidth = false; //是否填充 LinearLayout,最后 measure一次child判斷使用
boolean skippedMeasure = false; //是否跳過Measure過程
final int baselineChildIndex = mBaselineAlignedChildIndex;
final boolean useLargestChild = mUseLargestChild; //可以通過 xml 屬性 android:measureWithLargestChild 設(shè)置的
int largestChildHeight = Integer.MIN_VALUE;
int consumedExcessSpace = 0; //使用的剩余空間
int nonSkippedChildCount = 0; // 沒有跳過的 childView 個(gè)數(shù),用于分割線的邏輯
// See how tall everyone is. Also remember max width.
// 看看每個(gè) View 有多高,記錄最大寬度
for (int i = 0; i < count; ++i) {
final View child = getVirtualChildAt(i); //getChildAt()
if (child == null) {
mTotalLength += measureNullChild(i); //0
continue;
}
if (child.getVisibility() == View.GONE) {
i += getChildrenSkipCount(child, i); //0
continue;
}
nonSkippedChildCount++; // 沒有跳過的 childView 個(gè)數(shù)
if (hasDividerBeforeChildAt(i)) { //是否有分割線
mTotalLength += mDividerHeight; //加上分割線高度
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
totalWeight += lp.weight; //計(jì)算總權(quán)重 totalWeight
final boolean useExcessSpace = lp.height == 0 && lp.weight > 0; //是否使用剩余空間
//LinearLayout 的高度是固定的砰左,child 的 lp.height == 0 && lp.weight > 0匿醒,跳過 else
//步驟 skippedMeasure=true
if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) {
// 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.
//優(yōu)化:不要麻煩測量那些只使用多余空間的孩子。如果我們有空間分布缠导,這些視圖將在稍后得到度量廉羔。
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength + lp.topMargin + lp.bottomMargin);
skippedMeasure = true;
} else {
if (useExcessSpace) {
// The heightMode is either UNSPECIFIED or AT_MOST, and
// this child is only laid out using excess space. Measure
// using WRAP_CONTENT so that we can find out the view's
// optimal height. We'll restore the original height of 0
// after measurement.
//heightMode 是 UNSPECIFIED or AT_MOST, childView 只使用剩余空間
//使用WRAP_CONTENT進(jìn)行測量僻造,以便找出視圖的最佳高度憋他。測量后恢復(fù)原來高度 0
lp.height = LayoutParams.WRAP_CONTENT;
}
// Determine how big this child would like to be. If this or
// previous children have given a weight, then we allow it to
// use all available space (and we will shrink things later
// if needed).
//確定這個(gè)孩子想要多大。如果這個(gè)或前面的孩子給了一個(gè)權(quán)重髓削,那么我們允許它使用所有可用的空間
//(如果需要竹挡,我們將在稍后縮小內(nèi)容)。
final int usedHeight = totalWeight == 0 ? mTotalLength : 0;
//計(jì)算 childView 高度
measureChildBeforeLayout(child, i, widthMeasureSpec, 0,
heightMeasureSpec, usedHeight);
final int childHeight = child.getMeasuredHeight();
if (useExcessSpace) {
// Restore the original height and record how much space
// we've allocated to excess-only children so that we can
// match the behavior of EXACTLY measurement.
//恢復(fù)原來的高度立膛,并記錄我們分配給 excess-only children 的空間大小揪罕,以便我們能夠準(zhǔn)確地匹配測量的行為。
lp.height = 0;
consumedExcessSpace += childHeight;
}
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin +
lp.bottomMargin + getNextLocationOffset(child)); //getNextLocationOffset() 0
if (useLargestChild) {
largestChildHeight = Math.max(childHeight, largestChildHeight); //最大childHeight
}
}
/**
* If applicable, compute the additional offset to the child's baseline
* we'll need later when asked {@link #getBaseline}.
*/
if ((baselineChildIndex >= 0) && (baselineChildIndex == i + 1)) {
mBaselineChildTop = mTotalLength;
}
// if we are trying to use a child index for our baseline, the above
// book keeping only works if there are no children above it with
// weight. fail fast to aid the developer.
if (i < baselineChildIndex && lp.weight > 0) {
throw new RuntimeException("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.");
}
boolean matchWidthLocally = false;
if (widthMode != MeasureSpec.EXACTLY && lp.width == LayoutParams.MATCH_PARENT) {
// The width of the linear layout will scale, and at least one
// child said it wanted to match our width. Set a flag
// indicating that we need to remeasure at least that view when
// we know our width.
// 線性布局的寬度會(huì)縮放宝泵,并且至少有一個(gè)孩子說它想要匹配我們的寬度好啰。
// 設(shè)置一個(gè)標(biāo)志,表明當(dāng)我們知道寬度時(shí)儿奶,至少需要重新測量該視圖坎怪。
//LinearLayout 的寬度是 WRAP_CONTENT
matchWidth = true;
matchWidthLocally = true;
}
final int margin = lp.leftMargin + lp.rightMargin;
final int measuredWidth = child.getMeasuredWidth() + margin;
maxWidth = Math.max(maxWidth, measuredWidth);
childState = combineMeasuredStates(childState, child.getMeasuredState());
allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT;
if (lp.weight > 0) {
/*
* Widths of weighted Views are bogus if we end up
* remeasuring, so keep them separate.
*/
//如果我們最終重新測量加權(quán)視圖的寬度,那么它就是假的廓握,所以要把它們分開搅窿。
weightedMaxWidth = Math.max(weightedMaxWidth,
matchWidthLocally ? margin : measuredWidth);
} else {
alternativeMaxWidth = Math.max(alternativeMaxWidth,
matchWidthLocally ? margin : measuredWidth);
}
i += getChildrenSkipCount(child, i); //0
}//第一次循環(huán)結(jié)束 獲得 alternative weightedMaxWidth maxWidth matchWidth largestChildHeight
if (nonSkippedChildCount > 0 && hasDividerBeforeChildAt(count)) {
mTotalLength += mDividerHeight;
}
//useLargestChild 屬性指定
//所以接下來根據(jù) largestChildHeight 重新計(jì)算高度
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
// 負(fù) margins
final int totalLength = mTotalLength;
mTotalLength = Math.max(totalLength, totalLength + largestChildHeight +
lp.topMargin + lp.bottomMargin + getNextLocationOffset(child));
}
}//第二個(gè)循環(huán) useLargestChild 使用 largestChildHeight 計(jì)算 mTotalLength
// Add in our padding
mTotalLength += mPaddingTop + mPaddingBottom;
int heightSize = mTotalLength;
// Check against our minimum height
//檢查 最低高度
heightSize = Math.max(heightSize, getSuggestedMinimumHeight()); //(mBackground == null) ? mMinHeight : max(mMinHeight, mBackground.getMinimumHeight());
// Reconcile our calculated size with the heightMeasureSpec
//將我們計(jì)算出的尺寸與高度測量值進(jìn)行比對(duì)
int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0);
// 通過 heightMeasureSpec嘁酿,調(diào)整 heightSize 的大小,具體的過程需要
// 看一下 resolveSizeAndState() 方法的實(shí)現(xiàn)
// 經(jīng)過調(diào)整之后就是 LinearLayout 的大小了
heightSize = heightSizeAndState & MEASURED_SIZE_MASK;
// 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.
// 重新計(jì)算有 weight 屬性的 childView 大小男应,
// 如果還有可用的空間闹司,則擴(kuò)展 childView,計(jì)算其大小
// 如果 childView 超出了 LinearLayout 的邊界沐飘,則收縮 childView
int remainingExcess = heightSize - mTotalLength
+ (mAllowInconsistentMeasurement ? 0 : consumedExcessSpace); //剩余空間
if (skippedMeasure
|| ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) {
//// 根據(jù) mWeightSum 計(jì)算得到 remainingWeightSum游桩,mWeightSum 是通過
// `android:weightSum` 屬性設(shè)置的,totalWeight 是通過第一次 for 循環(huán)計(jì)算得到的
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;
// 這是設(shè)置了 weight 的情況下耐朴,最重要的一行代碼
// remainingExcess 剩余高度 * ( childView 的 weight / remainingWeightSum)
// share 便是此 childView 通過這個(gè)公式計(jì)算得到的高度借卧,
// 并重新計(jì)算剩余高度 remainingExcess 和剩余權(quán)重總和 remainingWeightSum
if (childWeight > 0) {
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);
// Child may now not fit in vertical dimension.
childState = combineMeasuredStates(childState, child.getMeasuredState()
& (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
} //child measure
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?
} 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 (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child == null || child.getVisibility() == View.GONE) {
continue;
}
final LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) child.getLayoutParams();
float childExtra = lp.weight;
if (childExtra > 0) {
child.measure(
MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(),
MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(largestChildHeight,
MeasureSpec.EXACTLY));
}
}
}
}
if (!allFillParent && widthMode != MeasureSpec.EXACTLY) {
maxWidth = alternativeMaxWidth;
}
maxWidth += mPaddingLeft + mPaddingRight;
// Check against our minimum width
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
heightSizeAndState);
if (matchWidth) {
forceUniformWidth(count, heightMeasureSpec);
}
}
/**
* 再次計(jì)算寬度
*/
private void forceUniformWidth(int count, int heightMeasureSpec) {
// Pretend that the linear layout has an exact size.
int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(),
MeasureSpec.EXACTLY);
for (int i = 0; i< count; ++i) {
final View child = getVirtualChildAt(i);
if (child != null && child.getVisibility() != GONE) {
LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams)child.getLayoutParams());
if (lp.width == LayoutParams.MATCH_PARENT) {
// Temporarily force children to reuse their old measured height
// FIXME: this may not be right for something like wrapping text?
int oldHeight = lp.height;
lp.height = child.getMeasuredHeight();
// Remeasue with new dimensions
measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0);
lp.height = oldHeight;
}
}
}
}
void layoutVertical(int left, int top, int right, int bottom) {
final int paddingLeft = mPaddingLeft;
int childTop;
int childLeft;
// Where right end of child should go
final int width = right - left;
int childRight = width - mPaddingRight;
// Space available for child
int childSpace = width - paddingLeft - mPaddingRight;
final int count = getVirtualChildCount();
final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
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;
}
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;
setChildFrame(child, childLeft, childTop + getLocationOffset(child),
childWidth, childHeight);
childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);
i += getChildrenSkipCount(child, i);
}
}
}
void drawDividersVertical(Canvas canvas) {
final int count = getVirtualChildCount();
for (int i = 0; i < count; i++) {
final View child = getVirtualChildAt(i);
if (child != null && child.getVisibility() != GONE) {
//判斷當(dāng)前View之前是否要繪制分割線
if (hasDividerBeforeChildAt(i)) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int top = child.getTop() - lp.topMargin - mDividerHeight;
drawHorizontalDivider(canvas, top);
}
}
}
//判斷是否要繪制最后的分割線
if (hasDividerBeforeChildAt(count)) {
final View child = getLastNonGoneChild();
int bottom = 0;
if (child == null) {
bottom = getHeight() - getPaddingBottom() - mDividerHeight;
} else {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
bottom = child.getBottom() + lp.bottomMargin;
}
drawHorizontalDivider(canvas, bottom);
}
}