The previous article talked about how the Android activity boot was performed to the OnCreate method in the handler, and the handler principle in Android This article is mainly from the OnCreate method we have to write the method Setcontentview start, study the layout or how the view is loaded into the mobile phone window.
When executing to Setcontentview, the actual execution is
public void Setcontentview (int layoutresid) { GetWindow (). Setcontentview (LAYOUTRESID); Initactionbar ();}
You can see that the Setcontentview method is actually the window class
Private window Mwindow;public window GetWindow () { return mwindow;}
The window class is an abstract class, and the following is primarily to find his implementation class. Mwindow initialization in
final void attach (...) { ... Mwindow = Policymanager.makenewwindow (this); Mwindow.setcallback (this); Mwindow.getlayoutinflater (). Setprivatefactory (this); ... }
The Attach method is in the main method. See the previous blog for details. A static method of the Policymanager class was called to generate the Window object Makenewwindow
private static final String Policy_impl_class_name = "Com.android.internal.policy.impl.Policy";p rivate static Final ipolicy spolicy;static { try { Class policyclass = Class.forName (policy_impl_class_name); Spolicy = (ipolicy) policyclass.newinstance (); } ... } public static Window Makenewwindow (context context) { return Spolicy.makenewwindow (context);}
You can see that the Window object is obtained through the Makenewwindow method of the policy class, which is an instance of the policy obtained through the reflection mechanism.
Public Window Makenewwindow (context context) { return new Phonewindow (context);}
Can see is actually a phonewindow. Well, according to polymorphism, it's actually called Phonewindow#setcontentwindow.
public void Setcontentview (int layoutresid) { if (mcontentparent = = null) { Installdecor ();//1 } else { Mcontentparent.removeallviews (); } Mlayoutinflater.inflate (Layoutresid, mcontentparent);//2 will fill Layoutresid, His father view is Mcontentparent is inside the Installdecor method mcontentparent = Generatelayout (Mdecor); get final Callback cb = Getcallback (); if (CB! = null &&!isdestroyed ()) { cb.oncontentchanged ();} }
Execute to Installdecor ()
private void Installdecor () { if (Mdecor = = null) { Mdecor = Generatedecor ();//Generate decorative window, decorate window inherits from Framelayout Mdecor.setdescendantfocusability (viewgroup.focus_after_descendants); Mdecor.setisrootnamespace (true); if (!minvalidatepanelmenuposted && minvalidatepanelmenufeatures! = 0) { mdecor.postonanimation ( minvalidatepanelmenurunnable); } } if (mcontentparent = = null) { mcontentparent = generatelayout (Mdecor);//Generate layout, return to the parent layout, for the time being, see the code in detail Mdecor.makeoptionalfitssystemwindows (); Mtitleview = (TextView) Findviewbyid (com.android.internal.r.id.title); ...... } }}
The code for Generatelayout is as follows
Protected ViewGroup generatelayout (Decorview decor) {//Apply data from current theme. TypedArray a = Getwindowstyle ();//Gets the theme of the current setting ... if (A.getboolean (Com.android.internal.r.styleable.window_windown Otitle, False)) {requestfeature (feature_no_title);//You can see that the windows in the Androidmanifest configuration are actually changed here in the code} else if (A.getboolean (Com.android.internal.r.styleable.window_windowactionbar, False)) {//Don ' t allow an action BA R If there is no title. Requestfeature (Feature_action_bar); } if (A.getboolean (Com.android.internal.r.styleable.window_windowactionbaroverlay, False)) {Requestfeat Ure (Feature_action_bar_overlay); }......//19-63 the default layout according to the various window styles we have specified such as untitled,//These layouts are available in D:\SoftWare\Java\android4.2-source\frameworks\base\core\res\ res\layout int layoutresource; int features = Getlocalfeatures (); 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; } removefeature (Feature_action_bar); } else if ((Features & (1 << feature_progress) | (1 << feature_indeterminate_progress))) ! = 0 && (features & (1 << feature_action_bar)) = = 0) {Layoutresource = com.an droid.internal.r.layout.screen_progress; } else if ((Features & (1 << feature_custom_title)) = 0) {if (misfloating) {Typedva Lue res = new Typedvalue (); GetContext (). Gettheme (). Resolveattribute (Com.android.internal.r.attr.dialogcustomtitledecOrlayout, Res, true); Layoutresource = Res.resourceid; } else {layoutresource = Com.android.internal.r.layout.screen_custom_title; } removefeature (Feature_action_bar); } else if ((Features & (1 << feature_no_title) = = 0) {if (misfloating) {Typedvalue res = new Typedvalue (); GetContext (). Gettheme (). Resolveattribute (Com.android.internal.r.attr.dialogtitledecorlayout, Res, true); Layoutresource = Res.resourceid; } else if ((Features & (1 << feature_action_bar)) = 0) {if (Features & (1 << FEATURE _action_bar_overlay))! = 0) {Layoutresource = Com.android.internal.r.layout.screen_action_bar_overlay; } else {layoutresource = Com.android.internal.r.layout.screen_action_bar; }} else {layoutResource = Com.android.internal.r.layout.screen_title; }} else if ((Features & (1 << feature_action_mode_overlay)) = 0) {Layoutresource = Com.andr Oid.internal.r.layout.screen_simple_overlay_action_mode; } else {layoutresource = com.android.internal.r.layout.screen_simple; } mdecor.startchanging (); View in = Mlayoutinflater.inflate (Layoutresource, NULL),//Layoutresource filled into view Decor.addview (in, new Vie) based on the above decision Wgroup.layoutparams (Match_parent, match_parent));//The AddView method that invokes the adornment window adds the view generated in the previous step to the outermost decorative window ViewGroup contentparent = (ViewGroup) Findviewbyid (id_android_content);//id_android_content corresponds to @android:id/content is actually a framelayout ... mdecor.finishchanging (); return contentparent; }
Below are the most commonly used layoutresource layout files They are under the D:\SoftWare\Java\android4.2-source\frameworks\base\core\res\res\layout folder
Screen-title.xml
<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_ba R "android:layout_width=" Match_parent "android:layout_height=" wrap_content "/> <framela Yout android:layout_width= "match_parent" android:layout_height= "? Android:attr/windowtitlesize" style = "? Android:attr/windowtitlebackgroundstyle" > <textview android:id= "@android: Id/title" style= "? and Roid:attr/windowtitlestyle "android:background=" @null "android:fadingedge=" Horizontal "an Droid:gravity= "center_vertical" android:layout_width= "match_parent" android:layout_height= "Match_par Ent "/> </framelayout> <framelayout 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:attr/windowcontentoverlay "/></linearlayout>
The outermost layer is a linear layout that contains the frame layout (which contains a textview is actually the title) and the frame layout. In fact, this is the most common look. This is what the default generated program looks like.
Screen-simple.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:fitssystemwindows=" true " android:orientation= "Vertical" > <viewstub android:id= "@+id/action_mode_bar_stub" android:inflatedid= "@+id/action_mode _bar " android:layout=" @layout/action_mode_bar " android:layout_width=" match_parent " android:layout_ height= "Wrap_content"/> <framelayout android:id= "@android: id/content" android:layout_width = "Match_parent" android:layout_height= "match_parent" android:foregroundinsidepadding= "false" Android:foregroundgravity= "Fill_horizontal|top" android:foreground= "? Android:attr/windowcontentoverlay"/ ></LinearLayout>
Obviously this is the layout that is loaded by default when the full screen is set.
We can see that the framelayout with the ID content exists because he wants to load the XML that we populate
In Screen-title.xml, for example, this is roughly the case.
Through the above analysis, the following points are understood:
1. The interface that the activity presents is actually a Phonewindow class that is managed, and there is a Decorview member in the class that is the outermost container.
2. This diagram above is very important and shows the structure of the window.
3. What the hell is activity? It's not good to be clear ... ^_^ in short, it's different from the first.
4. If you don't know the problem, check the source code. Code is the best teacher!
How layout view is loaded onto the phone window