Android--measurespec Study-turn

Source: Internet
Author: User

 Original address: http://blog.csdn.net/yuhailong626/article/details/20639217When customizing the view and ViewGroup, we often encounter the int type Measurespec to represent the size of a component, which is not only the size of the component, but also the size of the model.

This size pattern is a little hard to understand. There are three types of component sizes in the system:

1. Precision Mode (measurespec.exactly)

In this mode, the value of the dimension is what, then the length or width of the component is how much.

2. Maximum mode (measurespec.at_most)

This is the parent component, can give the maximum space, the current component length or width of the largest can only be so large, of course, can be smaller than this.

3. No mode specified (measurespec.unspecified)

This means that the current component is free to use space and is unrestricted.

There may be a lot of people who can't figure out how an int integer can represent two things (size mode and size value), an int type We know there are 32 bits. And there are three patterns, to represent three states, at least 2 bits of bits. The system uses the highest 2-bit representation mode.

A maximum of two bits is 00 when the "unspecified mode" is indicated. namely Measurespec.unspecified

The maximum of two bits is 01 when the "precision mode" is indicated. namely measurespec.exactly

Up to two bits is 11 when the maximum mode is indicated. namely Measurespec.at_most

A lot of people meet in place Operation Head is big, for easy to operate, so the system provides me with a Measurespec tool class.

This tool class has four methods and three constants (shown above) for our use:

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 int makemeasurespec (int size, int mode)

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

Public Static int getmode (int measurespec)

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

Public Static int getsize (int measurespec)

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

Public Static String toString (int measurespec)

measurespec.exactly: When we specify a control's Layout_width or Layout_height as a specific value, such as andorid:layout_width= "50dip", or fill_parent Yes, is a case where the size of the control has been determined, and 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.

Therefore, when overriding the Onmeasure method, the dimensions are calculated differently depending on the pattern. The following code is a more typical approach:

[Java]View PlainCopy 
  1. @Override
  2. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  3. Setmeasureddimension (Getmeasuredlength (Widthmeasurespec, True), Getmeasuredlength (Heightmeasurespec,       false));
  4. }
  5. Private int getmeasuredlength (int length, boolean iswidth) {
  6. int specmode = measurespec.getmode (length);
  7. int specsize = measurespec.getsize (length);
  8. int size;
  9. int padding = iswidth? Getpaddingleft () + getpaddingright ()
  10. : Getpaddingtop () + Getpaddingbottom ();
  11. if (Specmode = = measurespec.exactly) {
  12. size = Specsize;
  13. } Else {
  14. Size = Iswidth? padding + mwave.length/ 4:default_height
  15. + padding;
  16. if (Specmode = = measurespec.at_most) {
  17. Size = math.min (size, specsize);
  18. }
  19. }
  20. return size;
  21. }

Workaround for ScrollView nested listview and GridView Conflicts

[HTML]View PlainCopy 
  1. public class Mylistview extends ListView {
  2. Public Mylistview (Context context) {
  3. Super (context);
  4. }
  5. Public Mylistview (context context, AttributeSet Attrs) {
  6. Super (context, attrs);
  7. }
  8. Public Mylistview (context context, AttributeSet attrs, int defstyle) {
  9. Super (context, attrs, Defstyle);
  10. }
  11. @Override
  12. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  13. int Expandspec = measurespec.makemeasurespec (integer.max_value >> 2,
  14. Measurespec.at_most);
  15. Super.onmeasure (Widthmeasurespec, Expandspec);
  16. }
  17. }
  18. public class MyGridView extends GridView {
  19. Private Boolean Havescrollbar = true;
  20. Public MyGridView (Context context) {
  21. Super (context);
  22. }
  23. Public MyGridView (context context, AttributeSet Attrs) {
  24. Super (context, attrs);
  25. }
  26. Public MyGridView (context context, AttributeSet attrs, int defstyle) {
  27. Super (context, attrs, Defstyle);
  28. }
  29. /**
  30. * Set whether there is a scrollbar and should be set to False when you want to display it in Scollview. Default is True
  31. *
  32. * @param havescrollbars
  33. */
  34. public void Sethavescrollbar (Boolean havescrollbar) {
  35. This.havescrollbar = Havescrollbar;
  36. }
  37. @Override
  38. protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
  39. if (Havescrollbars = = False) {
  40. int Expandspec = measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most);
  41. Super.onmeasure (Widthmeasurespec, Expandspec);
  42. } else {
  43. Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
  44. }
  45. }
  46. }


Android--measurespec Study-turn

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.