Definitions in View.java:
Public final void measure (int widthmeasurespec,int heightmeasurespec) {
...
Onmeasure
...
}
protected void onmeasure (int widthmeasurespec,int heightmeasurespec) {
Setmeasureddimension (Getdefaultsize (Getsuggestedminimumwidth (), Widthmeasurespec),
Getdefaultsize (Getsuggestedminimumheight (), heightmeasurespec));
}
public static int getdefaultsize (int size, int measurespec) { int result = size; int specmode = Measurespec.getmode (measurespec); int specsize = measurespec.getsize (measurespec); Switch (specmode) {case measurespec.unspecified://Unspecified result = size; break; Case Measurespec.at_most://At most case measurespec.exactly://Precision result = specsize; break; } return result; }
In the ViewGroup:
protected void Measurechildren (int widthmeasurespec, int heightmeasurespec) { final int size = Mchildrencount; Final view[] children = Mchildren; for (int i = 0; i < size; ++i) { final View child = Children[i]; if ((Child.mviewflags & visibility_mask)! = GONE) { measurechild (child, Widthmeasurespec, Heightmeasurespec); c7/>}}}
protected void Measurechild (View child, int parentwidthmeasurespec, int parentheightmeasurespec) { final Layoutparams LP = Child.getlayoutparams (); Final int childwidthmeasurespec = Getchildmeasurespec (Parentwidthmeasurespec, mpaddingleft + mpaddingright, Lp.width); Final int childheightmeasurespec = Getchildmeasurespec (Parentheightmeasurespec, mpaddingtop + MPaddingBottom, Lp.height); Child.measure (Childwidthmeasurespec, Childheightmeasurespec); }
protected void Measurechildwithmargins (View child, int parentwidthmeasurespec, int widthused, int Parentheightmeasurespec, int heightused) { final marginlayoutparams LP = (marginlayoutparams) Child.getlayoutparams (); Final int childwidthmeasurespec = Getchildmeasurespec (Parentwidthmeasurespec, mpaddingleft + mpaddingright + Lp.leftmargin + lp.rightmargin + widthused, lp.width); Final int childheightmeasurespec = Getchildmeasurespec (Parentheightmeasurespec, mpaddingtop + MPaddingBottom + Lp.topmargin + lp.bottommargin + heightused, lp.height); Child.measure (Childwidthmeasurespec, Childheightmeasurespec); }
Description:
1. Measure is the final modified method and cannot be overridden.
when called externally, call view.measure (int wspec, int hspec) directly.
Onmeasure was called in measure.
When customizing the view, rewrite the onmeasure.
2.Measurespec This is a combination of mode and size that does not require our specific care.
When measuring, the measurespec.getsize|getmode can be called to get the corresponding size and mode.
Then use Measurespec.makemeasurespec (Size,mode); To create a Measurespec object.
So how does mode come in? is based on the Layoutwith|height parameter when using the custom view, so you can't just make a new one yourself.
the size can be specified by itself, or the measurespec.getsize can be used directly .
3. If you are a view, rewrite onmeasure to be aware of:
If you are using a custom view, wrap_content is used. Then call setmeasureddimension in the Onmeasure ,
To specify the width height of the view. If a fill_parent or a specific DP value is used. Then you can use Super.onmeasure directly.
4. If you are a viewgroup, be careful when overriding onmeasure:
First, the above two is combined to measure the height of its own.
Then, you need to measure the height of the sub-view.
The ways to measure sub-view are:
Getchildat (int index). You can get the Sub view on index.
The number of child view is obtained by Getchildcount, and then the child view is iterated through.
Next,subview.measure (int wspec, int hspec); Measuring methods using the sub view itself
Or call ViewGroup's method of measuring sub-view:
//A sub-view, how wide, how high, with the internal viewgroup padding value
Measurechild (subView, int wspec, int hspec);
//All sub-view is how wide, how high, internally called the Measurechild method
Measurechildren (int wspec, int hspec);
//A sub-view, how wide, how high, with the viewgroup padding value, margin value, and incoming width-height wused, hused
Measurechildwithmargins (subView, intwspec, int wused, int hspec, int hused);
Onmeasure, Measure, Measurechild, measurechildren some brief description