ViewGroup definition
In the API, this describes ViewGroup: a viewgroup is a special view, can contain other views. By meaning we can understand that other UI controls, including our custom controls, can be included in ViewGroup
Advantages and Uses
We understand that the most important purpose of a UI control is to use it. To understand the control, you must understand the advantages of the control. ViewGroup is relatively complex in Android UI controls, it is highly customizable, and with Declare-styleable custom control properties, almost every convenience of a control can be controlled.
With these advantages in mind, where do you think you can find ViewGroup? Think about it and I'll talk about my understanding at the end of the article.
Drawing process
There are two and important callback methods in ViewGroup
- onmeasure (int widthmeasurespec, int heightmeasurespec)
- OnLayout (Boolean changed, int left, int top, int. right, int bottom)
Onmeasure
Ask all children to measure themselves and compute the measurement of this layout based on the children.
OnLayout
Position all children within this layout.
The above is the API in the interpretation of two methods, the real use of onmeasure and onlayout can be called multiple times, according to log we will find that onlayout are not called after Onmeasure, which confirms the API's statement, ViewGroup the numerical value before drawing, and then draws the control to the corresponding place according to the corresponding value.
Getmeasuredwidth
This method here alone to say, in the Onmeasure method to calculate the location of the control, we will use the Getmeasuredwidth () and Getmeasuredheight (), the resulting values include padding, not including margin, Be careful when calculating.
Android ViewGroup Usage Summary