View layout of Android GUI

Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left"><span style="font-family:‘Microsoft YaHei‘; font-size:14px">After the first step in the view drawing mechanism is clear, we continue to understand the second process of analyzing view drawing, which is layout positioning. Continue to follow the analysis source, according to the previous process analysis we know that the view is drawn from the Performtraversals method of rootviewimpl, in this method called performmeasure, performlayout, Performdraw and other methods to measure, layout, draw, then we will look at the performlayout in the side of what has been done, the key source of the method is as Follows:</span></p></p><pre><pre><pre name="code" class="java">private void performlayout (windowmanager.layoutparams lp, int desiredwindowwidth, int Desiredwindowheight) { mlayoutrequested = false; Mscrollmaychange = true; Minlayout = true; Final View host = mView; ... Try { host.layout (0, 0, host.getmeasuredwidth (), host.getmeasuredheight ()), ... } finally { Trace.traceend (trace.trace_tag_view); } Minlayout = false; }</pre></pre></pre><p align="left"><p align="left"><span style="font-family:‘Microsoft YaHei‘; font-size:14px">It is seen that the most critical code is called the Host.layout method, then we do not remember what the host is a thing? yes, That's exactly what we said before about the root view decorview. So let's go back to Decorview and see what it did in the layout method. It is disappointing that we did not find the method in the decorview, do not worry, according to this class of inheritance system, we finally traced to the layout method in View.</span></p></p><p align="left"><p align="left"></p></p><pre><pre name="code" class="java">public void layout (int l, int t, int r, int b) {if (MPRIVATEFLAGS3 & pflag3_measure_needed_before_layout)! = 0) {onmeasure (moldwidthmeasurespec, moldheightmeasurespec); MPRIVATEFLAGS3 &= ~pflag3_measure_needed_before_layout; } int oldl = mleft; int Oldt = mtop; int oldb = mbottom; int OLDR = mright; Boolean changed = Islayoutmodeoptical (mparent)? Setopticalframe (l, t, r, b): setframe (l, t, r, b); If (changed | | (mprivateflags & pflag_layout_required) = = Pflag_layout_required) {onlayout (changed, l, t, r, b); Mprivateflags &= ~pflag_layout_required; Listenerinfo Li = mlistenerinfo; If (li! = null && li.monlayoutchangelisteners! = Null) {arraylist<onlayoutchangelistener> li Stenerscopy = (arraylist<onlayoutchangelistener>) Li.mOnLayoutChangeListeners.clone (); InchT numlisteners = listenerscopy.size (); for (int i = 0; i < numlisteners; ++i) {listenerscopy.get (i). onlayoutchange (this, l, t, r, b, oldl, oldt, oldr, oldb); }}} mprivateflags &= ~pflag_force_layout; MPRIVATEFLAGS3 |= pflag3_is_laid_out; }protected void onlayout (boolean changed, int left, int top, int. right, int Bottom) {}</pre></pre><p align="left"><p align="left"><span style="font-family:‘Microsoft YaHei‘; font-size:14px">The 4 parameters in this method represent the distance between the current view and the parent view in 4 directions, and as can be seen from the description, this method should not be overridden by the quilt class, and if a re-layout is required, the method that can be overridden in the subclass is onlayout, which is an empty method in view and nothing is Written. The actual layout method is not identified as final in view, which means it can be Overridden.</span></p></p><p align="left"><p align="left"><span style="font-family:‘Microsoft YaHei‘; font-size:14px">Continue to look at the relevant code in viewgoup, the layout has been rewritten and added the final identity, while the onlayout is identified as an abstract method, so inheriting the ViewGroup class is, can not override the layout method, and to implement the OnLayout Method. As you can see from the code, although ViewGroup overrides layout, it essentially invokes the layout of the view and then, by calling the OnLayout method, finally finishes Positioning.</span></p></p><pre><pre><pre name="code" class="java"> @Override public final void layout (int l, int t, int r, int b) { if (!msuppresslayout && (mtransition = = N ull | | !mtransition.ischanginglayout ())} { If (mtransition! = Null) { mtransition.layoutchange (this); } Super.layout (l, t, r, b); } Else { //record The fact that we NoOp ' d it; request layout when transition finishes mlayoutcalledwhilesuppressed = true; } } @Override protected abstract void OnLayout (boolean changed, int l, int t, int r, int b);</pre></pre></pre><p align="left"><p align="left"><span style="font-family:‘Microsoft YaHei‘; font-size:14px">In the decorview, and did not find onlayout method, so it is definitely used in its parent class framelayout, find Framelayout source code, You can view the OnLayout method, as Follows:</span></p></p><pre><pre name="code" class="java"> @Override protected void onlayout (boolean changed, int left, int top, int. right, int Bottom) {layoutchildren ( left, top, right, bottom, false/* no force left gravity */); } void Layoutchildren (int left, int top, int. right, int bottom, Boolean Forceleftgravi Ty) {final int count = Getchildcount (); Final int parentleft = Getpaddingleftwithforeground (); Final int parentright = Right-left-getpaddingrightwithforeground (); Final int parenttop = Getpaddingtopwithforeground (); Final int parentbottom = Bottom-top-getpaddingbottomwithforeground (); mforegroundboundschanged = true; for (int i = 0; i < count; i++) {final View child = Getchildat (i); If (child.getvisibility () = GONE) {final layoutparams LP = (layoutparams) Child.getlayoutparams (); Final int width = child.getmeasuredwidth (); Final int height = ChilD.getmeasuredheight (); int childleft; int childtop; int gravity = lp.gravity; If (gravity = =-1) {gravity = default_child_gravity; } final int layoutdirection = Getlayoutdirection (); Final int absolutegravity = gravity.getabsolutegravity (Gravity, layoutdirection); Final int verticalgravity = gravity & gravity.vertical_gravity_mask; Switch (absolutegravity & gravity.horizontal_gravity_mask) {case gravity.center_horizontal: Childleft = parentleft + (PARENTRIGHT-PARENTLEFT-WIDTH)/2 + lp.leftmargin- lp.rightmargin; Break Case Gravity.RIGHT:if (!forceleftgravity) {childleft = parentright-w idth-lp.rightmargin; Break } Case Gravity.LEFT:default:childLeft = parentleft + LP . leftMargin; } switch (verticalgravity) {case Gravity.TOP:childTop = Parentt OP + lp.topmargin; Break Case Gravity.CENTER_VERTICAL:childTop = parenttop + (PARENTBOTTOM-PARENTTOP-HEIGHT)/2 + lp.topmargin-lp.bottommargin; Break Case Gravity.BOTTOM:childTop = parentbottom-height-lp.bottommargin; Break Default:childtop = Parenttop + lp.topmargin; } child.layout (childleft, childtop, childleft + width, Childtop + height); } } }</pre></pre><p><p><span style="font-family:‘Microsoft YaHei‘; font-size:14px">From the method we can see that the Layoutchildren method is finally called in framelayout, where each view in the container is laid out according to the measurement results and some layout attributes. Based on the above code analysis, we can draw a diagram of the view layout positioning as Follows.</span></p></p><p align="left"><p align="left"></p></p><p><p></p></p><p><p><span style="font-family:‘Microsoft YaHei‘; font-size:15px">Inquiries or technical exchanges, Please join the official QQ group: (452379712)<br></span></p></p><p><p></p></p>Jerry Education<br>Source:<span style="color:#000000"><span style="color:#000000"><span style="color:#000000">http://blog.csdn.net/jerehedu/</span></span></span><br><span style="font-family:‘Microsoft YaHei‘; color:#000000"><span style="font-family:‘Microsoft YaHei‘; color:#000000">This article is the copyright of <span style="color:#000000">Yantai Jerry Education Technology co., Ltd.</span> and CSDN common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. </span></span><p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>View layout of Android GUI</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.