First, Introduction
The Android system must measure the view before drawing the view, which tells the system how much view to draw. This process is performed in the Onmeasure () method. The Android system provides the Measurespec class by helping us measure the view.
The measurement mode can be divided into three types:
- Exactly: that is the exact value mode, the system uses the exactly mode by default.
- At_most: The maximum mode, when the control's Layout_width property or Layout_height property is specified as Wrap_content, the size of the control generally varies with the control's sub-space or content. The size of this control can be as long as it does not exceed the maximum size allowed by the parent control.
- UNSPECIFIED: It does not specify its size measurement mode, how large the view is, and is normally used when drawing a custom view.
The default Onmeasure () method of the view class supports only exactly mode, so you can only use exactly mode when customizing the control without overriding the Onmeasure () method.
With the Measurespec class, you can get the measurement mode of the view and the size that the view wants to draw. With this information, you can control the size of the view's last display.
Second, examples
1 @Override 2 protected void onmeasure (intint heightmeasurespec)3{4 Super. Onmeasure (Widthmeasurespec, heightmeasurespec); 5 }
Measurement of Android View