How to understand the Onmeasure,onlayout process under Android

Source: Internet
Author: User

In Android view how to finish drawing this process introduces a lot, but a lot of theoretical things, recently re-organized, popular explanation.

The view drawing process is like you're lending to a bank,

In the execution of Onmeasure, like the bank tells you about the amount of the loan? You do all the calculations according to your needs, calculate a figure that you probably need, and then tell the question how much you need to borrow. The loan amount is like showing the size of space.

The actual code execution process is like this

onmeasure (int widthmeasurespec, int heightmeasurespec), where Widthmeasurespec and heightmeasurespec are the approximate amount that the bank tells. After the internal calculation is complete, the actual desired size is returned to the parent view by setmeasureddimension (width, height).

That is, onmeasure is used to determine the size of the view display (by calling the measure of the child view to determine the size of the child view)

In the execution of OnLayout, it is like the bank receives your loan request, according to its own reserves and your qualifications, wholesale the amount of the loan and the time of collection. The time to pick up is like the starting position of the display, and the quota is the size of the display space.

The actual code execution process is like this

OnLayout (Boolean changed, int left, int top, int. right, int bottom)-determines where the view appears in the parent class, calculated by the position of the child view, Set the position in the parent class to the child view by invoking the layout of the child view.

When executing Dispatchdraw, it's like you go to a bank to get a real loan, get the money you need, sign all kinds of contracts and procedures, and complete your loan process.

The Dispatchdraw process is to draw what you need to draw within the specified space, and you can draw the inner sub-view by Drawchild.

There are three scenarios in mode, with values of

Measurespec.unspecified,measurespec.exactly,measurespec.at_most.


Measurespec.exactly is the exact size when we specify the control's Layout_width or Layout_height as a specific value, as
Andorid:layout_width= "50dip", or fill_parent Yes, is a case where the size of the control has been determined, all are exact dimensions.

Measurespec.at_most is the maximum size, and when the control's layout_width or layout_height is specified as wrap_content, the size of the control generally varies with the control's subspace or content. The size of the control can be as long as it does not exceed the maximum allowable size of the parent control. Therefore, mode at this point is at_most,size gives the maximum size allowed by the parent control.

Measurespec.unspecified is not a specified size, this is not a lot, it is generally the parent control is Adapterview, passed through the measure method of the mode.

You can call the Setmeasureddimenson method to pass in the height and width of the view, set the actual size of the child view, and tell the parent control how much space to place the child view.

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.