Android MeasuerSpce origin and usage

Source: Internet
Author: User

Android MeasuerSpce origin and usage

Description: MeasuerSpce is a combination of measured values (size) and mode (mode) transmitted by parent to child.

Use Cases: We often process the spec in the child onMeasure (int widthMeasureSpec, int heightMeasureSpec) function to determine the length and width of the child.

Here we can see that MeasureSpec has a Measurement Mode of 3:

MeasureSpec. AT_MOST: The maximum parent size of child. This type is usually attributed to "wrap_content ".

MeasureSpec. EXACTLY: The size of child is a definite value, which is usually attributed to "match_parent" or a definite value.

MeasureSpec. UNSPECIFED: This is almost unnecessary. If this parameter is not specified, child can obtain any desired size.

MakeMeasureSpec, getMode, and getSize of Measure are just a single-bit operation.

 

After talking about it for a long time, how did we get this widthMeasureSpec and heightMeasureSpec? This requires a look at the source code.

We know that the measurement and drawing of a View are triggered by its parent, so we directly go to the ViewGroup

The entry point is measure:

Here we see a parentWidthMeasureSpec and parentHeightSpec. Here we don't care about it. We probably know that this is the spec given by its parent.

Continue to enter the function:

The switch (mode) is similar to the onMeasure of the view.

Continue down:

By now, the spec has been combined, and the next step is to pass it to child computing.

So far, the measurement of viewgroup is complete. The next step is to hand over the computation to the child and return to our onMeasure function.

We also mentioned the parentWidthMeasureSpec and parentHeightSpec, which are the measurement modes and values given by the current viewgroup parent. Follow the steps above.

 

So in the future, it will be easy to solve this problem.

View view = getLayoutInflater (). inflate (R. layout. layout_item, null );

Toast. makeText (MainActivity. this, "view_w =" + view. getMeasuredWidth () + "," + view. getLayoutParams (), 0). show ();

Why is the view width 0 and layoutparams null?

The width of the view is 0, which is obvious: the view does not have a measure, because it is a view parsed from a pull xml file. It does not have a parent, nor does it have a parent measurement, so it is 0.

The solution is measure, view. measure (0, 0); we pass 0, 0, and finally the view itself measures itself. The layoutparams parameter is null. Why?

We know that layoutParams is the layout parameter provided by parent to child. In the view source code, we can see that

The get and set methods are also available.

Let's go back to viewGroup.

 

Here we can see that addview will pass a params. How does this params come from?

Here we can see that child layoutparams is assigned in addview, so the layoutinflate view above has no layoutparams because there is no parent.

Solution:

Thank you for your correction and criticism!

Related Article

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.