@Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
  super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  int sizeWidth=MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
  int sizeHeight=MeasureSpec.getSize(heightMeasureSpec);
  int modeWidth=MeasureSpec.getMode(widthMeasureSpec);
  int modeHeight=MeasureSpec.getMode(heightMeasureSpec);
  int width=0;
  int height=getPaddingTop() + getPaddingBottom();
  int lineWidth=0;
  int lineHeight=0;
  int childCount=getChildCount();
  for (int i=0; i < childCount; i++) {
    View child=getChildAt(i);
    boolean lastChild=i == childCount - 1;
    if (child.getVisibility() == View.GONE) {
      if (lastChild) {
        width=Math.max(width,lineWidth);
        height+=lineHeight;
      }
      continue;
    }
    measureChildWithMargins(child,widthMeasureSpec,lineWidth,heightMeasureSpec,height);
    LayoutParams lp=(LayoutParams)child.getLayoutParams();
    int childWidthMode=MeasureSpec.AT_MOST;
    int childWidthSize=sizeWidth;
    int childHeightMode=MeasureSpec.AT_MOST;
    int childHeightSize=sizeHeight;
    if (lp.width == LayoutParams.MATCH_PARENT) {
      childWidthMode=MeasureSpec.EXACTLY;
      childWidthSize-=lp.leftMargin + lp.rightMargin;
    }
 else     if (lp.width >= 0) {
      childWidthMode=MeasureSpec.EXACTLY;
      childWidthSize=lp.width;
    }
    if (lp.height >= 0) {
      childHeightMode=MeasureSpec.EXACTLY;
      childHeightSize=lp.height;
    }
 else     if (modeHeight == MeasureSpec.UNSPECIFIED) {
      childHeightMode=MeasureSpec.UNSPECIFIED;
      childHeightSize=0;
    }
    child.measure(MeasureSpec.makeMeasureSpec(childWidthSize,childWidthMode),MeasureSpec.makeMeasureSpec(childHeightSize,childHeightMode));
    int childWidth=child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
    if (lineWidth + childWidth > sizeWidth) {
      width=Math.max(width,lineWidth);
      lineWidth=childWidth;
      height+=lineHeight;
      lineHeight=child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
    }
 else {
      lineWidth+=childWidth;
      lineHeight=Math.max(lineHeight,child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
    }
    if (lastChild) {
      width=Math.max(width,lineWidth);
      height+=lineHeight;
    }
  }
  width+=getPaddingLeft() + getPaddingRight();
  setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width,(modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
}

@ Override protected void onMeasure ( int widthMeasureSpec , int heightMeasureSpec ) { super . onMeasure ( widthMeasureSpec , heightMeasureSpec ) ; int sizeWidth = MeasureSpec . getSize ( widthMeasureSpec ) - getPaddingLeft ( ) - getPaddingRight ( ) ; int sizeHeight = MeasureSpec . getSize ( heightMeasureSpec ) ; int modeWidth = MeasureSpec . getMode ( widthMeasureSpec ) ; int modeHeight = MeasureSpec . getMode ( heightMeasureSpec ) ; int width = 0 ; int height = getPaddingTop ( ) + getPaddingBottom ( ) ; int lineWidth = 0 ; int lineHeight = 0 ; int childCount = getChildCount ( ) ; for ( int i = 0 ; i < childCount ; i ++ ) { View child = getChildAt ( i ) ; boolean lastChild = i == childCount - 1 ; if ( child . getVisibility ( ) == View . GONE ) { if ( lastChild ) { width = Math . max ( width , lineWidth ) ; height += lineHeight ; } continue ; } measureChildWithMargins ( child , widthMeasureSpec , lineWidth , heightMeasureSpec , height ) ; LayoutParams lp = ( LayoutParams ) child . getLayoutParams ( ) ; int childWidthMode = MeasureSpec . AT_MOST ; int childWidthSize = sizeWidth ; int childHeightMode = MeasureSpec . AT_MOST ; int childHeightSize = sizeHeight ; if ( lp . width == LayoutParams . MATCH_PARENT ) { childWidthMode = MeasureSpec . EXACTLY ; childWidthSize -= lp . leftMargin + lp . rightMargin ; } else if ( lp . width >= 0 ) { childWidthMode = MeasureSpec . EXACTLY ; childWidthSize = lp . width ; } if ( lp . height >= 0 ) { childHeightMode = MeasureSpec . EXACTLY ; childHeightSize = lp . height ; } else if ( modeHeight == MeasureSpec . UNSPECIFIED ) { childHeightMode = MeasureSpec . UNSPECIFIED ; childHeightSize = 0 ; } child . measure ( MeasureSpec . makeMeasureSpec ( childWidthSize , childWidthMode ) , MeasureSpec . makeMeasureSpec ( childHeightSize , childHeightMode ) ) ; int childWidth = child . getMeasuredWidth ( ) + lp . leftMargin + lp . rightMargin ; if ( lineWidth + childWidth > sizeWidth ) { width = Math . max ( width , lineWidth ) ; lineWidth = childWidth ; height += lineHeight ; lineHeight = child . getMeasuredHeight ( ) + lp . topMargin + lp . bottomMargin ; } else { lineWidth += childWidth ; lineHeight = Math . max ( lineHeight , child . getMeasuredHeight ( ) + lp . topMargin + lp . bottomMargin ) ; } if ( lastChild ) { width = Math . max ( width , lineWidth ) ; height += lineHeight ; } } width += getPaddingLeft ( ) + getPaddingRight ( ) ; setMeasuredDimension ( ( modeWidth == MeasureSpec . EXACTLY ) ? sizeWidth : width , ( modeHeight == MeasureSpec . EXACTLY ) ? sizeHeight : height ) ; }
ask all children to measure themselves and compute the measurement of this layout based on the children .
