Quick introduction to Android onMeasure () onLayout () and onmeasureonlayout
Override ViewGroup to learn the onMeasure () onLayout () method
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
// Obtain the mode and size. There are three boundary parameters: UNSPECIFIED is generally 0, EXACTLY is accurate, and AT_MOST is adaptive.
Int w_mode = MeasureSpec. getMode (widthMeasureSpec );
Int w_size = MeasureSpec. getSize (widthMeasureSpec );
Int h_mode = MeasureSpec. getMode (heightMeasureSpec );
Int h_size = MeasureSpec. getSize (heightMeasureSpec );
// Calculate the size of all custom child Controls
MeasureChildren (widthMeasureSpec, heightMeasureSpec );
// Notify the parent control of the size of the Child control.
// SetMeasuredDimension (resolveSize (size, widthMeasureSpec), resolveSize (size, heightMeasureSpec ));
SetMeasuredDimension (w_size, h_size );
Log. e ("onMeasure", "wide mode =" + w_mode + "wide size =" + w_size
+ "High mode =" + h_mode + "high size =" + h_size );
// Super. onMeasure (widthMeasureSpec, heightMeasureSpec );
}
// OnLayout is used to specify the display position of the view. The order of method execution is after onMeasure, because the view must be placed only when the size is known.
@ Override
Protected void onLayout (boolean changed, int l, int t, int r, int B ){
// Record height
Int mTotalHeight = 0;
// Traverse all child views
Int childCount = getChildCount ();
For (int I = 0; I <childCount; I ++ ){
View childView = getChildAt (I );
// Obtain the view size calculated in onMeasure
Int measureHeight = childView. getMeasuredHeight ();
Int measuredWidth = childView. getMeasuredWidth ();
ChildView. layout (l, mTotalHeight, measuredWidth, mTotalHeight + measureHeight );
MTotalHeight + = measureHeight;
}
}