When inheriting the ViewGroup class, you must override two methods: onMeasure and onLayout.
1. Call the setMeasuredDimension method in onMeasure.
Void android. view. View. setMeasuredDimension (int measuredWidth, int measuredHeight)
In onMeasure (int, int), you must call setMeasuredDimension (int width, int height) to store the width and height of the measurement. If you do not do so, an exception IllegalStateException is triggered.
2. Call the child's measure Method in method onMeasure.
Void android. view. View. measure (int widthMeasureSpec, int heightMeasureSpec)
This method is used to measure the view size. The parent view uses the width and height parameters to provide constraint information. In fact, the measurement of view is completed in the onMeasure (int, int) method. Therefore, only the onMeasure (int, int) method can and must be overwritten. The widthMeasureSpec parameter provides the horizontal space specification of the view, and the heightMeasureSpec parameter provides the vertical space specification of the view.
3. parse the onMeasure (int, int) Method
Void android. view. View. onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Measure the view and its content to determine the width and height of the view. This method is called in measure (int, int) and must be overwritten to accurately and effectively measure the view content.
When rewriting this method, you must call setMeasuredDimension (int, int) to store the measured width and height values. If the execution fails, an IllegalStateException is triggered. OnMeasure (int, int) of the parent view is valid.
By default, the basic measurement data of a view takes its background size unless a larger size is allowed. The child view must override onMeasure (int, int) to provide a more accurate measurement value. If overwritten, The subclass ensures that the measured height and width are at least the smallest height and width of the view (obtained through getSuggestedMinimumHeight () and getSuggestedMinimumWidth ).
4. parse the onLayout (boolean, int, int) method.
Void android. view. ViewGroup. onLayout (boolean changed, int l, int t, int r, int B)
Call scenario: it is called when the view sets the size and position for the child. Child view, including child, must overwrite the onLayout (boolean, int) method and call their respective layout (int, int) methods.
Parameter description: The changed parameter indicates that the view has a new size or position. The l parameter indicates the Left position relative to the parent view. The t parameter indicates the Top position relative to the parent view; parameter r indicates the Right position relative to the parent view; parameter B indicates the Bottom position relative to the parent view ..
5. parse the View. MeasureSpec class
Android. view. View. MeasureSpec
The MeasureSpec object encapsulates the layout Specification Description and passes it from the parent view to the Child view. Each MeasureSpec object represents the width or height specification.
The MeasureSpec object contains a size and a mode. The mode can take one of the following three values:
- UNSPECIFIED, 1073741824 [0x40000000]. If no rule is added, no rule is added to the subview.
- EXACTLY, 0 [0x0], which indicates that the parent view determines the exact size for the Child view.
- AT_MOST,-2147483648 [0x80000000]. The child view can be as large as possible within the specified size.