Measurespec use of Custom view in Android

Source: Internet
Author: User

In general, the custom control overrides the view's Onmeasure method, because it specifies the size of the control on the screen.

protected void onmeasure (int widthmeasurespec, int heightmeasurespec)

Onmeasure the two parameters passed in are the size passed in by the previous control, and in many cases, overriding the method requires the actual size of the calculated control, and then calls setmeasureddimension (int, int) to set the actual size.

Onmeasure incoming Widthmeasurespec and heightmeasurespec are not general dimensional values, but are values that combine patterns and dimensions. We need to get the pattern by int mode = Measurespec.getmode (Widthmeasurespec), with int size = Measurespec.getsize (widthmeasurespec ) to get the dimensions.

Mode has a total of three cases, the value is Measurespec respectively. UNSPECIFIED, measurespec.exactly, Measurespec.at_most.

1, measurespec.exactly is the exact size, when we specify the control's Layout_width or Layout_height as a specific value, such as andorid:layout_width= "50dip", or Fill_ The parent is a case where the size of the control has been determined, all of which are exact dimensions.

2, Measurespec.at_most is the maximum size, 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.

3, measurespec.unspecified is not specified size, this situation is not much, is generally the parent control is adapterview, through the measure method passed in mode.

Methods are:

This is the size and pattern given by us to generate an int variable containing both of these information , here This pattern parameter, pass one of three constants.

Public Static intMakemeasurespec (intsize,int mode)

This is to get the schema information represented in this variable, and compare the resulting value to three constants.

Public Static intGetMode (intMeasurespec)

This is the value that gets the size of the dimension represented in this variable.

Public Static intGetSize (intMeasurespec)

return the pattern and size of this variable into a string to make it easy to log

  public Static  string  tostring ( int  measurespec)

Measurespec use of Custom view in Android

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.