Quick introduction to Android onMeasure () onLayout () and onmeasureonlayout

Source: Internet
Author: User

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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.