Viewgroup.measurechildren ()
Viewgroup.measurechild ()
viewgroup.measurechildwithmargins ()
/** * Ask One of the children of this view to measure itself, taking into * account both the Measurespec Requi Rements for this view and its padding * and margins. The child must has marginlayoutparams the heavy lifting is * do in Getchildmeasurespec. * * @param child-measure * @param parentwidthmeasurespec the width requirements for this view * @ param widthused Extra space that have been used up by the parent * horizontally (possibly by other children of T He parent) * @param parentheightmeasurespec The height requirements for this view * @param heightused Extra Space That have been used up by the parent * vertically (possibly by other children of the parent) */protected void Measurechildwithmargins (View child, int parentwidthmeasurespec, int widthused, int parentheigh Tmeasurespec, int heightused) {//1 type conversion final Marginlayoutparams LP = (marginlayoutparams) child.getlayoUtparams ();//2 Getchildmeasurespec method Final int childwidthmeasurespec = Getchildmeasurespec (Parentwidthmeasurespec, Mpaddingleft + mpaddingright + lp.leftmargin + lp.rightmargin + widthused, lp.width ); Final int childheightmeasurespec = Getchildmeasurespec (parentheightmeasurespec, Mpaddingtop + MPaddingBotto m + lp.topmargin + lp.bottommargin + heightused, lp.height); Child.measure (Childwidthmeasurespec, Childheightmeasurespec); }
This method measure operations on the child view passed by the first parameter.
1. Passed child view Layoutparams must be or inherit from Marginlayoutparams, no will throw a conversion exception
/** * Does the hard part of measurechildren:figuring out the Measurespec to * pass to a particular child. This method figures out the right Measurespec * for one dimension (height or width) of one child view. * * The goal is to combine information from we measurespec with the * Layoutparams of the The "the best P" Ossible results. For example, * If the this view knows it size (because its measurespec have a mode of * exactly), and the child ha s indicated in their layoutparams that it wants * to is the same size as the parent, the parent should ask the child to * Layout given an exact size. * @param spec The requirements for this view * @param padding the padding of the "This" for the current dimension and * margins, if applicable * @param childdimension How big the child wants to being in the current * Dimension * @return A measurespec integer for the child */public static int GetchildmeasUrespec (int spec, int padding, int childdimension) {int specmode = Measurespec.getmode (spec); int specsize = measurespec.getsize (spec); int size = Math.max (0, specsize-padding); int resultsize = 0; int resultmode = 0; Switch (Specmode) {///Parent has imposed a exact size on US case MeasureSpec.EXACTLY:if (child Dimension >= 0) {resultsize = childdimension; Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.fill_parent) {//child wants to is our size. So is it. resultsize = size; Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size. It can ' t be//bigger than us. resultsize = size; Resultmode = Measurespec.at_most; } break; //Parent have imposed a maximum size on US case MeasureSpec.AT_MOST:if (childdimension >= 0) { Child wants a specific size ... so is it resultsize = childdimension; Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.fill_parent) {//child wants Fixed. Constrain bigger than us. resultsize = size; Resultmode = Measurespec.at_most; } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size. It can ' t be//bigger than us. resultsize = size; Resultmode = Measurespec.at_most; } break; Parent asked to see how big we want to is case MeasureSpec.UNSPECIFIED:if (childdimension >= 0) { Child wants a sPecific size ... let him has it resultsize = childdimension; Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.fill_parent) {//child wants to is our size ... find out how big It should/be resultsize = 0; Resultmode = measurespec.unspecified; } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size .... find How about//big it should be resultsize = 0; Resultmode = measurespec.unspecified; } break; } return Measurespec.makemeasurespec (ResultSize, Resultmode); }
protected void Measurechildwithmargins (View child, int parentwidthmeasurespec, int widthused, int Parentheightmeasurespec, int heightused) {//1 ....//2 Getchildmeasurespec method Final int childwidthmeasurespec = Getch Ildmeasurespec (Parentwidthmeasurespec, mpaddingleft + mpaddingright + lp.leftmargin + lp.rightmargin + widthused, lp.width);}
Call this method to see the exact meaning of these three parameters.
1. Spec: This view requires a specification of "Parentwidthmeasurespec"
2. padding: The padding value of the current ViewGroup view and the margin value of the incoming child view "Mpaddingleft + mpaddingright + lp.leftmargin + lp.rightmargin + Widthus Ed "? Widthused
3. Childdimension: View of incoming parameters, expect to show how big "lp.width"
* * To cite an example
Extension: Measurespec method and Stitching mode
/** * Utility to reconcile a desired size with constraints imposed by a measurespec. * would take the desired size, unless a different size was imposed by the constraints. * * @param size How big the view wants to is * @param measurespec Constraints imposed by the parent * @return The size this view should is. * /public static int resolvesize (int size, int measurespec) { int result = size; int specmode = Measurespec.getmode (measurespec); int specsize = measurespec.getsize (measurespec); Switch (specmode) {case measurespec.unspecified: result = size; break; Case Measurespec.at_most: result = math.min (size, specsize); break; Case measurespec.exactly: result = specsize; break; } return result; }
/** * Creates a measure specification based on the supplied size and mode. * * The mode must always be one of the following: * <ul> * <li>{@link Android.view.View.Measu Respec#unspecified}</li> * <li>{@link android.view.view.measurespec#exactly}</li> * <li>{@link android.view.view.measurespec#at_most}</li> * </ul> * * @ param size The size of the measure specification * @param mode the mode of the measure specification * @return T He measure specification based on size and mode * /public static int Makemeasurespec (int size, int mode) { return size + mode; }
/** * <p>this Mehod must be called by {@link #onMeasure (int, int.)} to store the * measured width and mea sured height. Failing to does so would trigger an * exception at measurement time.</p> * * @param measuredwidth the Measu Red width of this view * @param measuredheight The measured height of this view */protected final void Setmeas ureddimension (int measuredwidth, int measuredheight) {mmeasuredwidth = Measuredwidth; Mmeasuredheight = Measuredheight; Mprivateflags |= Measured_dimension_set; }/** * The width of this view as measured in the recent call to measure (). * This should is used during measurement and layout calculations only. Use * {@link #getWidth ()} "to" see how wide a view was after layout. * * @return The measured width of this view. */Public final int getmeasuredwidth () {return mmeasuredwidth; }/** * The height of this view as measured in the most receNT call to measure (). * This should is used during measurement and layout calculations only. Use * {@link #getHeight ()} "to" see how tall a view was after layout. * * @return The measured height of this view. */Public final int getmeasuredheight () {return mmeasuredheight; }
Android View measure (three) common methods