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
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- Setmeasureddimension (Getmeasuredlength (Widthmeasurespec, True), Getmeasuredlength (Heightmeasurespec, false));
- }
- Private int getmeasuredlength (int length, boolean iswidth) {
- int specmode = measurespec.getmode (length);
- int specsize = measurespec.getsize (length);
- int size;
- int padding = iswidth? Getpaddingleft () + getpaddingright ()
- : Getpaddingtop () + Getpaddingbottom ();
- if (Specmode = = measurespec.exactly) {
- size = Specsize;
- } Else {
- Size = Iswidth? padding + mwave.length/ 4:default_height
- + padding;
- if (Specmode = = measurespec.at_most) {
- Size = math.min (size, specsize);
- }
- }
- return size;
- }
Workaround for ScrollView nested listview and GridView Conflicts
[HTML]View PlainCopy
- public class Mylistview extends ListView {
- Public Mylistview (Context context) {
- Super (context);
- }
- Public Mylistview (context context, AttributeSet Attrs) {
- Super (context, attrs);
- }
- Public Mylistview (context context, AttributeSet attrs, int defstyle) {
- Super (context, attrs, Defstyle);
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- int Expandspec = measurespec.makemeasurespec (integer.max_value >> 2,
- Measurespec.at_most);
- Super.onmeasure (Widthmeasurespec, Expandspec);
- }
- }
- public class MyGridView extends GridView {
- Private Boolean Havescrollbar = true;
- Public MyGridView (Context context) {
- Super (context);
- }
- Public MyGridView (context context, AttributeSet Attrs) {
- Super (context, attrs);
- }
- Public MyGridView (context context, AttributeSet attrs, int defstyle) {
- Super (context, attrs, Defstyle);
- }
- /**
- * Set whether there is a scrollbar and should be set to False when you want to display it in Scollview. Default is True
- *
- * @param havescrollbars
- */
- public void Sethavescrollbar (Boolean havescrollbar) {
- This.havescrollbar = Havescrollbar;
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- if (Havescrollbars = = False) {
- int Expandspec = measurespec.makemeasurespec (integer.max_value >> 2, Measurespec.at_most);
- Super.onmeasure (Widthmeasurespec, Expandspec);
- } else {
- Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
- }
- }
- }
Android--measurespec Study-turn