What the layout process means
The layout process is the process by which the program uses the code of the layout file to calculate the actual size at run time.
What the layout process does
Two stages: The measurement phase and the layout stage.
Measurement Phase : Recursively invokes each view or ViewGroup's measure () method from top to bottom to measure their size and calculate their position; layout Stage : Recursively call each view or ViewGroup from top to bottom Layout () method to assign the measured size and position to them.
Layout process for View or ViewGroup
-
Measurement phase, measure () method called by parent View, after doing some preparation and optimization work in measure () , call onmeasure () for actual self-measurement. onmeasure () do, View and viewgroup different:
- View :
view in onmeasure () calculates its size and then saves it;
- viewgroup :
viewgroup in onmeasure () calls all child View &NBSP, measure () Let them measure themselves and calculate their actual size and position based on the desired dimensions computed by the Sub view (in fact, 99.99% of the parent view uses the child view The desired size is given as the actual size, as explained in the next or next period) and then saved. It also calculates its size and saves it according to the size and position of the child View;
-
In the layout phase, the layout() method is called by the parent view, where layout() it saves the parent view's own location and dimensions, and calls onLayout() to make the actual internal layout. onLayout()do things, View and ViewGroup also not the same:
- View: Because there is no child view, so do
View onLayout() nothing.
- viewgroup:
ViewGroup in onLayout() will call all of their own child View layout() methods, their size and position to them, let them complete their own internal layout.
How the layout process is customized
Three categories:
- Rewrite
onMeasure() to modify an existing View dimension;
- Rewrite
onMeasure() to customize View the size of the new custom;
- Rewrite
onMeasure() and customize onLayout() ViewGroup the internal layout to new customizations.
Specific practices for the first type of customization
That is, rewrite onMeasure() to modify the existing View size of the specific practice:
- Override the
onMeasure() method and invoke it inside super.onMeasure() , triggering the original self-measurement;
- In
super.onMeasure() the following use getMeasuredWidth() and getMeasuredHeight() to obtain the previous measurement results, and use their own algorithm, according to the measurement results calculated new results;
- Called
setMeasuredDimension() to save the new result.
New custom customizations for the second typeViewThe size
- The most important difference between a new custom size and a modified size
It is necessary to ensure that the calculation result satisfies the size limit given by the parent View while calculating
- Size limit for parent View
- Origin: The requirements of the developer (the
layout_ attributes that begin in the layout file) are more accurate after the parent View processing is calculated;
- Classification of restrictions:
UNSPECIFIED: No Limit
AT_MOST: Limit Caps
EXACTLY: Limit Fixed values
- New ways to define custom View dimensions
- Again
onMeasure() , and calculate the size of the View;
- Use
resolveSize() to have the child view's calculations conform to the limits of the parent view (of course, if you want to satisfy the limits of the parent view in your own way).
The third type of way to customize layout interior layouts
- Override
onMeasure() to calculate internal layout
- Rewrite
onLayout() to place child View
- Three steps to rewrite Onmeasure ():
- Call each child view
measure() to calculate the dimensions of the child view
- Calculates the location of the child view and saves the location and size of the child view
- Calculate your own size and
setMeasuredDimension() save
- Key to calculating sub-View dimensions
The key to calculating the size of a sub-view is the two parameters of the method-that is, the measure() two calculation of the child view MeasureSpec .
- The measurespec of the child View is calculated as follows:
- Combine the developer's requirements (the attributes that begin in the XML
layout_ ) and your own free space (your own size limit-used size)
- The size limit is based on your own
MeasureSpec mode.
- Exactly/at_most: The upper limit for
MeasureSpec the size ofsize
- UNSPECIFIED: No limit on size
- How to override OnLayout ()
onLayout()call each sub-View in layout() , and let them save their location and size.
Android Layout Basics