<span id="Label3"></p><p><p>Reprint please indicate source: http://blog.csdn.net/lmj623565791/article/details/41894125, This article from: "Zhang Hongyang's blog"</p></p><p><p>Everyone in the normal development. For Setcontentview certainly not unfamiliar, then the implementation of its internal will be more curious about it ~ ~ ~ lucky to finally see some phonewindow god horse source code, today take you to run a back to the source code ~ ~</p></p>1. Activity Setcontentview<p><p>first, Needless to say, entering the activity of Setcontentview</p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_1_5637288" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_1_5637288" name="code" class="java">public void Setcontentview (int layoutresid) { getWindow (). setcontentview (layoutresid); Initactionbar (); }</pre></pre><br><p><p>To be able to see inside gets the WINDOW. Then call the Window's Setcontentview</p></p>2, Phonewindow Setcontentview<p><p>The implementation class for window here is Phonewindow (package com.android.internal.policy.impl;). We look directly at its implementation:</p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_2_7337965" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_2_7337965" name="code" class="java">@Override public void Setcontentview (int layoutresid) { if (mcontentparent = = Null) { Installdecor (); } else { mcontentparent.removeallviews (); } Mlayoutinflater.inflate (layoutresid, mcontentparent); Final Callback cb = Getcallback (); If (cb! = null &&!isdestroyed ()) { cb.oncontentchanged ();} }</pre></pre><br>Can see, first infer whether Mcontentparent is null, is called Installdecor (), otherwise remove all its inner sub-views, We then put our incoming layout into mcontentparent by Layoutinflater.inflate.<p><p></p></p><p><p>From here we can see that mcontentparent is a viewgroup and wraps our entire layout file, and Installdecor () is expected to initialize our Mcontentparent. We're going to verify it in a Minute.</p></p><p><p></p></p><p><p>next, a callback object was obtained through GETCALLBACK. The fact that this callback is what we get is that we're doing the activity ourselves, and you can see that our activity is implementing the callback Interface.</p></p><p><p>This callback is obviously a callback that, when Phonewindow receives a system-distributed event related to its touch, IO, menu, and so on, can callback the corresponding activity for Processing. As for what methods callback can call back, look at the declaration method of this Interface.</p></p><p><p>Of course, This is not our key, because our Setcontentview is only callback oncontentchanged, and oncontentchanged in the activity is empty Implementation.</p></p><p><p></p></p><p><p>ok, then go to our Installdecor ()</p></p>3, Phonewindow Installdecor<p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_3_4022915" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_3_4022915" name="code" class="java">private void Installdecor () { if (mdecor = = Null) { Mdecor = Generatedecor (); Mdecor.setdescendantfocusability (viewgroup.focus_after_descendants); Mdecor.setisrootnamespace (true); //... } } if (mcontentparent = = Null) { mcontentparent = generatelayout (mdecor); Mtitleview = (TextView) Findviewbyid (com.android.internal.r.id.title); If (mtitleview! = Null) { //hides by feature_no_title, or sets the value of Mtitleview //... } else { Mactionbar = ( Actionbarview) Findviewbyid (com.android.internal.r.id.action_bar); If (mactionbar! = Null) { //set actionbar title, icon for God horse; based on feature initialize some display of Actionbar //... } }}</pre></pre><br>The code here is longer, removing some of the code that initializes the Actionbar-style God Horse.<p><p></p></p><p><p>Can see not only initialize the Mcontentparent here, and first call Generatedecor (), initialize a mdecor,mdecor is the Decorview object. is a subclass of Framelayout.</p></p><p><p>The way to get the focus after getting Mdecor is to acquire it only when its descendants are not Needed.</p></p><p><p>Then pass the Generatelayout (mdecor) and pass the Mdecor as the number of References. And then get to our mcontentparent;</p></p><p><p>next, you start to get the control through findviewbyid, and Here's the Findviewbyid code:</p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_4_6040018" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_4_6040018" name="code" class="java">Public View Findviewbyid (int id) { return getdecorview (). Findviewbyid (id); }</pre></pre><br>The Getdecorview returned is our Mdecor.<p><p></p></p><p><p></p></p><p><p></p></p><p><p>Here we're going to start by initializing the mdecor, and then initializing the mcontentparent through Mdecor. Next Mdecor will be able to use the Findviewbyid method. So I think that the method of initializing the Mdecor</p></p><p><p>Generatedecor (). It must have placed a layout or control for our Mdecor (the simplest is to use inflate to press the layout file). And mcontentparent may be a child view in the Mdecor.</p></p><p><p><br></p></p><p><p>Isn't that so?</p></p><p><p>Let's take a look first. implementation of the Generatedecor () method:</p></p>4, Phonewindow Generatedecor<p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_5_6977327" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_5_6977327" name="code" class="java">Protected Decorview Generatedecor () { return new Decorview (getcontext (),-1); }</pre></pre><pre code_snippet_id="553592" snippet_file_name="blog_20141216_6_4741289" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_6_4741289" name="code" class="java"> Public decorview (context context, int featureid) { Super (context); Mfeatureid = featureid; }</pre></pre><br><p><p>unfortunately, our Generatedecor () just initialized a framelayout object, and did not press inside the layout file, It seems we have some problems with the test, but nothing, since this method is not, then Generatelayout (mdecor); Layout file must be set in the And it's a very similar Name.</p></p>5, Phonewindow Generatelayout<p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_7_3726751" name="code" class="java">Protected ViewGroup generatelayout (decorview decor) {//Apply data from current Theme. TypedArray A = Getwindowstyle (); //... Window_windowisfloating,window_windownotitle. Window_windowactionbar///////first through the various properties set in Windowstyle. Requestfeature to Window or setflags if (a.getboolean (com.android.internal.r.styleable.window_windownotitle, false)) {requestfeature (feature_no_title); }//... if (a.getboolean (com.android.internal.r.styleable.window_windowfullscreen, false)) {setf Lags (flag_fullscreen, Flag_fullscreen & (~getforcedwindowflags ())); } //... Determine if menukey windowmanager.layoutparams params = getattributes () is required based on the current SDK version number; Set the mode of the Params.softinputmode soft keyboard with the properties set in a, And//assume that the current is a floating Activity. Set the Flag_dim_behind in the params and record the value of the Dimamount. As well as the Params.windowanimations record windowanimationstyle//inflate the window Decor. int layoutresource; int features = GetloCalfeatures (); System.out.println ("features:0x" + integer.tohexstring (Features)); If ((features & (1 << Feature_left_icon) | (1 << Feature_right_icon))) ! = 0) {if (misfloating) {typedvalue res = new Typedvalue (); GetContext (). gettheme (). resolveattribute (com.android.internal.r.attr.dialogtitleiconsdecorlayout, res, true); Layoutresource = res.resourceid; } Else {layoutresource = com.android.internal.r.layout.screen_title_icons; }//XXX Remove This once action bar supports these Features. Removefeature (feature_action_bar); System.out.println ("Title icons!"); } else if ((features & (1 << Feature_progress) | (1 << Feature_indeterminate_progress))) ! = 0 && (features & (1 << Feature_action_bar)) = = 0) {//special Case for a W Indow with Only a progress bar (and title). XXX need to has a no-title version of embedded windows. Layoutresource = com.android.internal.r.layout.screen_progress; System.out.println ("progress!"); } else if ((features & (1 << feature_custom_title)) = 0) {//special case for a window with a Custo M Title. If the window is floating, we need a dialog layout if (misfloating) {typedvalue res = new Ty Pedvalue (); GetContext (). gettheme (). resolveattribute (com.android.internal.r.attr.dialogcustomtitledecorlayout, res, true); Layoutresource = res.resourceid; } Else {layoutresource = com.android.internal.r.layout.screen_custom_title; }//XXX Remove This once action bar supports these Features. Removefeature (feature_action_bar); } else if ((features & (1 << Feature_no_tiTLE)) = = 0) {//If No other features and is embedded, only need a title. If the window is floating, we need a dialog layout if (misfloating) {typedvalue res = new Ty Pedvalue (); GetContext (). gettheme (). resolveattribute (com.android.internal.r.attr.dialogtitledecorlayout, res, true); Layoutresource = res.resourceid; } else if ((features & (1 << feature_action_bar)) = 0) {layoutresource = COM.ANDROID.INTERNAL.R . layout.screen_action_bar; } Else {layoutresource = com.android.internal.r.layout.screen_title; }//System.out.println ("title!"); } else if ((features & (1 << feature_action_mode_overlay)) = 0) {layoutresource = Com.android.inter Nal. r.layout.screen_simple_overlay_action_mode; } else {//Embedded, so no decoration is Needed. LayoutreSource = com.android.internal.r.layout.screen_simple; System.out.println ("simple!"); } View in = Mlayoutinflater.inflate (layoutresource, null); Decor.addview (in, new Viewgroup.layoutparams (match_parent, match_parent)); ViewGroup contentparent = (viewgroup) Findviewbyid (id_android_content); ... return contentparent; } }</pre><br>The code is also much Longer. First Getwindowstyle gets the properties defined in our window in the theme of the current WINDOW. Detailed References: \frameworks\base\core\res\res\values\attrs.xml<p><p></p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_8_7728721" name="code" class="html"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_8_7728721" name="code" class="html"> <!--the Set of attributes that describe a Windows ' s Theme. --<declare-styleable name= "window" > <attr name= "windowbackground"/> <attr name= "wind Owcontentoverlay "/> <attr name=" windowframe "/> <attr name=" windownotitle "/> <att R name= "windowfullscreen"/> <attr name= "windowoverscan"/> <attr name= "windowisfloating"/> <attr name= "windowistranslucent"/> <attr name= "windowshowwallpaper"/> <attr name= "wi Ndowanimationstyle "/> <attr name=" windowsoftinputmode "/> <attr name=" windowdisablepreview "/&G T <attr name= "windownodisplay"/> <attr name= "textcolor"/> <attr name= "backgrounddimenabled"/ > <attr name= "backgrounddimamount"/> </pre></pre>The values of these properties are then based On. To our window various requestfeature,setflags and so On.<p><p></p></p><p><p>So here is where we set theme for activity, and theme generally can be set in Androidmanifest.</p></p><p><p></p></p><p><p></p></p><p><p>The next step is the critical part. 21-75 Line: by the inference of features and misfloating, the Layoutresource is assigned, the value can be r.layout.screen_custom_title; r.layout.screen_action_bar, wait.</p></p><p><p>As for Features. In addition to theme, we were able to requestfeature before the Activity's OnCreate setcontentview, and explained that Why do I need to call Requestfeature before Setcontentview to set up fullscreen?</p></p><p><p></p></p><p><p>After getting layoutresource, 78 lines, through the Layoutinflater to transform the layout into a view, add to our decor, that is, the incoming Mdecor.</p></p><p><p>Next 81 Lines: pass through Mdecor.findviewbyid incoming r.id.content (believe This ID everybody more or less have heard of), return Mdecor (layout) The ID is content of view, general is Framelayout.</p></p><p><p>All right. Can see our Mdecor is a framelayout, and then according to theme to choose the layout file in the System. Convert the layout file to view through Inflate. Added to the Mdecor. These layout files include a framelayout with a content ID. Return its reference to Mcontentparent.</p></p><p><p>When our mcontentparent is worth it. Do you remember what you did? One more Phonewindow setcontentview.</p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_9_1174286" name="code" class="java"><pre code_snippet_id="553592" snippet_file_name="blog_20141216_9_1174286" name="code" class="java"> @Override public void Setcontentview (int layoutresid) { if (mcontentparent = = Null) { Installdecor (); } else { mcontentparent.removeallviews (); } Mlayoutinflater.inflate (layoutresid, mcontentparent); Final Callback cb = Getcallback (); If (cb! = null &&!isdestroyed ()) { cb.oncontentchanged ();} }</pre></pre><br>With the mcontentparent, we add the layout file we wrote to the Mcontentparent by Inflater.<p><p></p></p><p><p></p></p><p><p></p></p><p><p><br></p></p><p><p>About R.LAYOUT.XXX can be viewed inside the Frameworks\base\core\res\res\layout.</p></p><p><p>For Example: R.layout.screen_custom_title.xml</p></p><p><p></p></p><pre code_snippet_id="553592" snippet_file_name="blog_20141216_10_5176256" name="code" class="html"><?<p><p></p></p><p><p> XML version= "1.0" encoding= "utf-8"?><!--this was a custom layout for a screen.--><linearlayout xmlns: android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:fitssystemwindows= " True "> <!--popout Bar for action modes-<viewstub android:id=" @+id/action_mode_bar_stub " android:inflatedid= "@+id/action_mode_bar" android:layout= "@layout/action_mode_bar" android:lay Out_width= "match_parent" android:layout_height= "wrap_content"/> <framelayout android:id= "@android: I D/title_container "android:layout_width=" match_parent "android:layout_height="? </p></p><p><p>Android:attr/windowtitlesize "style="? android:attr/windowtitlebackgroundstyle "> </FrameLayout> <f Ramelayout android:id= "@android: id/content" android:layout_width= "match_parent" android:layout_height= "0dip "android:layout_weight=" 1 "android:foregroundgravity=" fill_horizontal|top "android:foreground="? Android Oid:attr/windowcontentoverlay "/></linearlayout></p></p></pre>The title_container above is a container for defining the title, and the following content is the container for placing the layout we set. About their own definition of the title example, we can Baidu Under.<p><p></p></p><p><p><br></p></p><p><p>here, Our Setcontentview analysis is Complete. We can recall:</p></p><p><p>First initialize the mdecor, that is, Decorview is a subclass of Framelayout. is the root view of our entire form.</p></p><p><p>And then. Select the appropriate layout according to the attribute values in Theme. By Infalter.inflater into our Mdecor.</p></p><p><p>In these layouts, actionbar is usually included. Title, and an ID of content Framelayout.</p></p><p><p>finally, the layout we set in the activity is infalter.inflater into our framelayout with the content of the ID.</p></p><p><p></p></p><p><p><br></p></p><p><p><br><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">----------------------------------------------------------------------------------------------------------</span><br></p></p><p><p></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Bo Master part of the video has been Launched. Let's say you don't like boring text. Please poke (first record, look forward to your support):</p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">1. Android self-defined Control scratch card in real-action e-commerce activities</span></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">2.</span> <span style="font-size: 24px;">Android self-defining controls to build Android streaming layouts and popular tags</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">3.</span> <span style="font-size: 24px;">Android robot "little mu" implementation</span><br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">4, High imitation QQ5.0 sideways slip<br></p></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="color: rgb(51, 102, 153); font-size: 24px;">5, High Imitation 5.2.1 main interface and message alert</span></p></p><p><p><span style="white-space: pre;"></span></p></p><p align="left"><p align="left"><br></p></p><br><p><p></p></p><br><br><p><p><br></p></p><p><p><br></p></p><p><p><br><br></p></p><p><p><br></p></p> <p><p>Setcontentview of Android Source code parsing</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