<span id="Label3"></p><p><p></p></p><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><br></span></span><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px">Viewgroup.measurechildren ()</span></span><br style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px">Viewgroup.measurechild ()</span></span><br style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px"><span style="font-family: Tahoma; orphans: 2; widows: 2; font-size: 14px">viewgroup.measurechildwithmargins ()</span></span><br><p><p></p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java"> /** * 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); }</pre><br><br><p><p></p></p><br><br><span style="white-space: pre"><span style="white-space: pre"></span></span>This method measure operations on the child view passed by the first parameter.<br><span style="white-space: pre"><span style="white-space: pre"></span></span>1. Passed child view Layoutparams must be or inherit from marginlayoutparams, No will throw a conversion exception<br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><pre name="code" class="java"> /** * 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); }</pre><br><br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><p><p><br></p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> 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);}</pre></pre><br><br><p><p></p></p><br><br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><span style="white-space: pre"><span style="white-space: pre"></span></span>Called at this Method. Take a look at the detailed meanings of these three parameters.<br><span style="white-space: pre"><span style="white-space: pre"></span></span>1. Spec: This view requires a specification of "parentwidthmeasurespec"<br><span style="white-space: pre"><span style="white-space: pre"></span></span>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<br><span style="white-space: pre"><span style="white-space: pre"></span></span>3. Childdimension: The view of the incoming parameters, expect to show how much "lp.width"<br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><span style="white-space: pre"><span style="white-space: pre"></span></span>* * For example<br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><span style="white-space: pre"><span style="white-space: pre"></span></span><br><p><p>Extension: Measurespec method and Stitching mode</p></p><p><p><br></p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> /** * 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; }</pre></pre><br><br><p><p></p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java"> /** * 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; }</pre></pre><br><br><p><p></p></p><p><p></p></p><pre name="code" class="java"> /** * <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; }</pre><br><br><p><p></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p> <p><p>Android View measure (iii) frequent usage</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service