Android Setcontentview () Source code parsing

Source: Internet
Author: User
<span id="Label3"></p>Objective<p><p>The general first sentence in activity is called <code>setContentView(R.layout.XXX)</code> , but which system does that work?</p></p><p><p>We know that after ClassLoader loaded the mainactivity, application was created first, followed by the Onattach and OnCreate () methods of the application Object. The first Activity's Onattach and OnCreate () methods are then called Sequentially. Probably have an impression, then the text will be Involved. Specific Reference: launcher Start the application process source Parsing.</p></p>New Test Engineering Testhierarchy<p><p>After you create a new project, there is only one relativelayout in activity_main.xml, which contains a textview.</p></p><p><p>Settings <code>android:id="@+id/myRelativeLayout"</code> , <code>android:id="@+id/myTextView"</code> .</p></p><p><p>Set mainactivity inherit from Activity.</p></p><p><p>Keep the default<code>MainActivity extends AppCompatActivity</code></p></p><p><p>Keep the default<code><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"></code></p></p><p><p>The initial work is Complete.</p></p>Using hierarchy to view the layout structure<p><p>Hierarchy is a visual layout analysis tool that is published with the SDK. Only the underlying view layout hierarchy is required Here. Since the hierarchy Viewer can only connect to the Android development phone or simulator, we first run the program on the virtual machine and then enter: \sdk\tools, Find hierarchyviewer.bat, Double-click. Then select our program, then click on Load View Hierarchy, then you will get a Heibuliuqiu view. And here, is the point to look at the Place. however, in order to facilitate understanding, omit some unnecessary tiltle and other elements, here to inherit activity as an example to Parse. You can try each view location by clicking on each node First.</p></p><p><p></p></p><p align="center"><p align="center">Mainactivity Inheriting activity</p></p><p><p>Notice here the top right corner of the two nodes, which is obviously activity_main.xml. no, Look at id!.</p></p>SOURCE parsing<p><p>SOURCE Location: Frameworks/base/core/java/android/app/activity.java</p></p><p><p>Activity#setcontentview ()</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">setContentView</span><span class="hljs-keyword">int</span> layoutResID) { getWindow().setContentView(layoutResID); initWindowDecorActionBar(); }</code></pre></pre><p><p>Since we inherited the activity, we omitted <code>initWindowDecorActionBar()</code> this step First. Activity Dafa is good ~. The next thing to focus on is a line of Code. First <code>getWindow()</code> of all, look what the ghost is Returning.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> <span class="hljs-keyword">public</span><span class="hljs-title">getWindow</span>() { <span class="hljs-keyword">return</span> mWindow; }</code></pre></pre><p><p>the <strong>Preface</strong> says that the first way to execute the activity is to attach (). The Mwindow here is instantiated in the Attach () method.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r"> <span class="hljs-keyword">attach</span>(<span class="hljs-keyword">...</span>){ mWindow = new PhoneWindow(this); mWindow.setCallback(this); }</code></pre></pre><p><p>Mwindow is a window object, but Phonewindow inherits from Window. A callback was set after obtaining the Mwindow through PHONEWINDOW. The activity implements the Window.callback interface, and the activity holds a reference to the window, which means that the activity can get a corresponding callback when invoking the callback interface Method. and activity can manipulate the view through the window Property. Follow up <code>getWindow().setContentView(layoutResID)</code> .</p></p><p><p>SOURCE Location: Frameworks/base/core/java/com/android/internal/policy/phonewindow.java</p></p><p><p>Phonewindow#setcontentview ()</p></p><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Setcontentview</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Layoutresid) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(mcontentparent = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {installdecor (); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(!hasfeature (FEATURE_CONTENT_TRANSITIONS)) {mcontentparent.removeallviews (); }<span class="hljs-comment"><span class="hljs-comment">//return false, Execute Else branch</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(hasfeature (FEATURE_CONTENT_TRANSITIONS)) {<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Scene newscene = scene.getsceneforlayout (mcontentparent, layoutresid, getcontext ()); Transitionto (newscene); }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{mlayoutinflater.inflate (layoutresid, mcontentparent); } mcontentparent.requestapplyinsets ();<span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Callback cb = Getcallback ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(cb! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>&&!isdestroyed ()) {cb.oncontentchanged (); } }</code></pre><p><p>First of all, The analysis of the code flow, first of all to determine whether Mcontentparent is empty, the first time come in nothing to do, definitely Null. The Feature_content_transitions property is used to set the toggle effect for activity, false by Default. The first call above <code>installDecor()</code> , from the context name, this method should be related to the mcontentparent Variable. It then calls the <code>mLayoutInflater.inflate(layoutResID, mContentParent)</code> padding we set up <code>R.layout.XXX</code> to Mcontentparent. In conjunction with the previous Hierarchyviewer diagram, Mcontentparent is an example of framelayout that contains activity_main.xml. The final callback <code>Callback#onContentChanged()</code> , where the CB is actually the activity Object. The implementation of this method in activity is empty, so we can replicate this method in our activity and implement our own Logic. The Oncontentchanged () method is called after the Activity's layout file has been changed, that is, call Setcontentview () or Addcontentview (). Follow Installdecor (), with the following emphasis on parsing Mdecor and Mcontentparent.</p></p><p><p>Phonewindow#installdecor ()</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r"> private void installDecor() { <span class="hljs-keyword">if</span> (mDecor == null) { mDecor = generateDecor(); <span class="hljs-keyword">...</span> } <span class="hljs-keyword">if</span> (mContentParent == null) { mContentParent = generateLayout(mDecor); <span class="hljs-keyword">...</span> } <span class="hljs-keyword">...</span>}</code></pre></pre><p><p>First call the Generatedecor () method to get the Mdecor instance, and then get to mcontentparent based on the Mdecor instance. Follow.</p></p><p><p>Phonewindow#generatedecor ()</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-keyword">protected</span><span class="hljs-title">generateDecor</span>() { <span class="hljs-keyword">return</span><span class="hljs-keyword">new</span> DecorView(getContext(), -<span class="hljs-number">1</span>); } <span class="hljs-keyword">private</span><span class="hljs-keyword">final</span><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DecorView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">FrameLayout</span></span></code></pre></pre><p><p>A Decorview object is directly new in Phonewindow#generatedecor (), and you can see that Decorview is just a viewgroup that inherits Framelayout. Follow Generatelayout () below.</p></p><p><p>Phonewindow#generatedecor ()</p></p><pre class="prettyprint"><code class=" hljs r">Protected ViewGroup generatelayout (decorview decor) {//get Custom Properties Window TypedArray A = Getwindowstyle ();<span class="hljs-keyword"><span class="hljs-keyword">...</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(a.getboolean (r.styleable.window_windownotitle, False)) {requestfeature (feature_no_title); }<span class="hljs-keyword"><span class="hljs-keyword">...</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>((features & (<span class="hljs-number"><span class="hljs-number">1</span></span><< feature_swipe_to_dismiss))! =<span class="hljs-number"><span class="hljs-number">0</span></span>) {layoutresource = r.layout.screen_swipe_dismiss; } ....<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{//Embedded, so no decoration are needed. Layoutresource = r.layout.screen_simple; System.out.println (<span class="hljs-string"><span class="hljs-string">"simple!"</span></span>); } mdecor.startchanging (); View<span class="hljs-keyword"><span class="hljs-keyword">inch</span></span>= Mlayoutinflater.inflate (layoutresource, null); Decor.addview (<span class="hljs-keyword"><span class="hljs-keyword">inch</span></span>, New Viewgroup.layoutparams (match_parent, match_parent)); Mcontentroot = (viewgroup)<span class="hljs-keyword"><span class="hljs-keyword">inch</span></span>; ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content);<span class="hljs-keyword"><span class="hljs-keyword">...</span></span>Mdecor.finishchanging ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>contentparent; }</code></pre><p><p>The function of a large segment omitted earlier is to get the various initialization work done after the custom properties window, <code>requestFeature(FEATURE_NO_TITLE)</code> for Example. Since this is done after this <code>View in = mLayoutInflater.inflate(layoutResource, null)</code> , the system is converted to view in based on the layout file of style, which is inherited by activity,style=theme.appcompat.light.darkactionbar, so the loaded layout is <code>screen_title.xml</code>. The in is then added to the mdecor, and the in is assigned to the Mcontentroot. Here's the <code>public static final int ID_ANDROID_CONTENT = com.android.internal.R.id.content</code> . The layout file Screen_title.xml looks like this:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-pi"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?></span></span><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout </span> <span class="hljs-attribute">xmlns: Android </span> =<span class="hljs-value"> "http://schemas.android.com/apk/res/android" </span> <span class=" Hljs-attribute ">android:layout_width </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:layout_height </span> =<span class=" hljs-value ">" match_parent "</span> <span class=" Hljs-attribute ">android:fitssystemwindows </span> =<span class=" hljs-value ">" true "</span> <span class="hljs-attribute">android:orientation </span> =<span class="hljs-value"> "vertical" </span>; </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">viewstub </span> <span class="hljs-attribute">android:id </span> =<span class="hljs-value"> "@+id/action_mode_bar_stub" </span> <span class="hljs-attribute">ANDROID:INFL Atedid </span> =<span class="hljs-value"> "@+id/action_mode_bar" </span> <span class="hljs-attribute">android:lay Out </span> =<span class="hljs-value"> "@layout/action_mode_bar" </span> <span class="hljs-attribute">android:lay Out_width </span> =<span class="hljs-value"> "match_parent" </span> <span class="hljs-attribute">android:layout_h Eight </span> =<span class="hljs-value"> "wrap_content" </span> <span class="hljs-attribute">android:theme </span> =<span class="hljs-value" and "attr actionbartheme" < span>/> </span></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">framelayout</span><span class="hljs-attribute">android:id</span>=<span class="hljs-value">"@android: id/content"</span><span class="hljs-attribute">android:layout_width</span>= <span class="hljs-value">"match_parent"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:foregroundinsidepadding</span>=<span class="hljs-value">"false" </span> <span class="hljs-attribute">android:foregroundgravity</span>= "<span class="hljs-value">fill_horizontal|top"</span><span class="hljs-attribute">android:foreground</span>=<span class="hljs-value">" ? android:attr/windowcontentoverlay "</span> /> </span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span></code></pre><p><p>The fragment with the content ID is contentparent, and the outermost linearlayout is MCONTENTVIEW. The ID is action_mode_bar_stub <code>android:visibility="gone"</code> . Finally release a picture of oneself mark ~</p></p><p><p></p></p><p><p><br></p></p> <blockquote> <blockquote> <p><strong>More framework source code analysis, please visit the framework Source code parsing series [catalog]</strong></p> </blockquote> </blockquote> <p><p>Android Setcontentview () Source code parsing</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.