Android custom streaming layout and android custom Layout
The advantage of stream layout is that the parent layout can automatically determine whether a child needs a line break and when a line break is required, so that the labels of the webpage version can be achieved. Today, we have made a simple custom streaming layout.
Specific results:
Principle:
In fact, it is very simple, Measure Layout. You only need to perform these two steps. Completely manually go to Measure Layout.
Let's take a look at the code.
The explanation is commented out in the code, because the format of the blog written for the note does not conform to the code format. You can refer to the specific source code. Finally, the source code is used.
1. Measure Measurement
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Int widthSize = MeasureSpec. getSize (widthMeasureSpec );
Int widthMode = MeasureSpec. getMode (widthMeasureSpec );
Int heightSize = MeasureSpec. getSize (heightMeasureSpec );
Int heightMode = MeasureSpec. getMode (heightMeasureSpec );
Int lineHeight = 0;
Int lineWidth = 0;
Int width = 0;
Int height = 0;
Int childCount = getChildCount ();
Log. I ("Test", getPaddingLeft () + "= right =" + getPaddingRight ());
For (int I = 0; I <childCount; I ++ ){
View childView = getChildAt (I );
MeasureChild (childView, widthMeasureSpec, heightMeasureSpec );
MarginLayoutParams params = (MarginLayoutParams) childView. getLayoutParams ();
Int childWidth = childView. getMeasuredWidth () + params. leftMargin + params. rightMargin;
Int childHeight = childView. getMeasuredHeight () + params. topMargin + params. bottomMargin;
If (lineWidth + childWidth)> widthSize-getPaddingLeft ()-getPaddingRight ()){
Width = Math. max (width, lineWidth );
LineWidth = childWidth;
Height + = lineHeight;
LineHeight = childHeight;
} Else {
LineWidth + = childWidth;
LineHeight = Math. max (lineHeight, childHeight );
}
If (I = childCount-1 ){
Width = Math. max (width, lineWidth );
Height + = lineHeight;
}
}
Height + = getPaddingTop () + getPaddingBottom ();
SetMeasuredDimension (widthMode = MeasureSpec. EXACTLY? WidthSize: width,
HeightMode = MeasureSpec. EXACTLY? HeightSize: height );
}
2. onLayout Layout
@ Override
Protected void onLayout (boolean a, int l, int t, int r, int B ){
ChildViewList. clear ();
Int childCount = getChildCount ();
Int width = getWidth ();
Int lineWidth = 0;
Int lineHeight = 0;
List <View> lineViews = new ArrayList <View> ();
For (int I = 0; I <childCount; I ++ ){
View childView = getChildAt (I );
MarginLayoutParams params = (MarginLayoutParams) childView. getLayoutParams ();
Int childWidth = childView. getMeasuredWidth () + params. leftMargin + params. rightMargin;
Int childHeight = childView. getMeasuredHeight () + params. topMargin + params. bottomMargin;
If (lineWidth + childWidth> width-getPaddingLeft ()-getPaddingRight ()){
ChildViewList. add (lineViews );
LineViews = new ArrayList <View> ();
If (I = 0 ){
LineHeight + = getPaddingTop ();
} Else if (I = childCount-1 ){
LineHeight + = getPaddingBottom ();
}
This. lineHeight. add (lineHeight );
LineHeight = 0;
LineWidth = 0;
}
LineWidth + = childWidth;
LineHeight = Math. max (lineHeight, childHeight );
LineViews. add (childView );
}
ChildViewList. add (lineViews );
This. lineHeight. add (lineHeight );
Int left = getPaddingLeft ();
Int top = getPaddingTop ();
For (int I = 0; I <childViewList. size (); I ++ ){
LineViews = childViewList. get (I );
For (int j = 0; j <lineViews. size (); j ++ ){
View childView = lineViews. get (j );
MarginLayoutParams params = (MarginLayoutParams) childView. getLayoutParams ();
Int lc = left + params. leftMargin;
Int tc = top + params. topMargin;
Int rc = lc + childView. getMeasuredWidth ();
Int bc = tc + childView. getMeasuredHeight ();
ChildView. layout (lc, tc, rc, bc );
Left + = params. leftMargin + childView. getMeasuredWidth () + params. rightMargin;
}
Left = getPaddingLeft ();
Top + = this. lineHeight. get (I );
}
}
Code:
Baidu Network Disk: http://pan.baidu.com/s/1hqH1kFU