Process Analysis for Android view loading into a form

Source: Internet
Author: User

Previous Blog Handler principle of Android in the H when the Andler When it comes to how Android's activity boot works to the OnCreate method, This article mainly from the OnCreate method we need to write the method Setcontentview start, study how layout view is loaded into the mobile phone form.

When you run to Setcontentview, you are actually running the

public void Setcontentview (int layoutresid) {        GetWindow (). Setcontentview (LAYOUTRESID);        Initactionbar ();}

To see the Setcontentview method that is actually the window class

Private window Mwindow;public window GetWindow () {        return mwindow;}

The window class is an abstract class, and the following is the main way 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 more 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 the window object that is obtained through the Makenewwindow method of the policy class. Here is an instance of the policy obtained through the reflection mechanism.

Public Window Makenewwindow (context context) {        return new Phonewindow (context);}

To be able to see is actually a phonewindow. Then the basis of polymorphism. In fact, it's called Phonewindow#setcontentwindow.

public void Setcontentview (int layoutresid) {        if (mcontentparent = = null) {            Installdecor ();//1        } else {            Mcontentparent.removeallviews ();        }        Mlayoutinflater.inflate (Layoutresid, mcontentparent);//2 fills the 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 ();}        }

Run to Installdecor ()

private void Installdecor () {    if (Mdecor = = null) {        Mdecor = Generatedecor ();//Generate decorative form. Decoration forms inherit 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, the temporary understanding, in detail to see the Code        mdecor.makeoptionalfitssystemwindows ();        Mtitleview = (TextView) Findviewbyid (com.android.internal.r.id.title);        ......        }    }}

Generatelayout code such as the following

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);//Can see the form that is usually configured in Androidmanifest, etc. in fact, in the code is changed here} else if (A.getboolean (Com.android.internal.r.styleable.window_windowactionbar, False)) {//Don ' t allow an action B            Ar 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); The}......//19-63 line is given a default layout based on the various form styles we have specified, such as the title or not.        These layouts are 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),//based on the above inferred selected Layoutresource filled into view Decor.addview (in, New Vie Wgroup.layoutparams (Match_parent, match_parent));//Call the AddView method of the adornment form to add the view generated in the previous step to the outermost decorative form ViewGroup contentparent        = (ViewGroup) Findviewbyid (id_android_content)//id_android_content the corresponding @android:id/content is actually a framelayout        ... mdecor.finishchanging ();    return contentparent; }

The following are the most frequently used layoutresource layout files They are in the D:\SoftWare\Java\android4.2-source\frameworks\base\core\res\res\layout directory

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= "?

Android:attr/windowtitlestyle "android:background=" @null "android:fadingedge=" Horizontal " Android:gravity= "center_vertical" android:layout_width= "match_parent" android:layout_height= "Match_ Parent "/> </FrameLayout> <framelayout android:id=" @android: Id/content "Android:layout_width=" mat Ch_parent "android:layout_height=" 0dip "android:layout_weight=" 1 "android:foregroundgravity=" Fill_ho Rizontal|top "android:foreground="? Android:attr/windowcontentoverlay "/></linearlayout>

The outermost layer is a linear layout. Includes frame layout (including a textview is actually the title) and frame layout. In fact, this is the most common appearance.

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>

It's obvious that this is the layout that is loaded by default in full screen settings.

We can see that the framelayout with the content ID exists, because he wants to load our populated XML

According to Screen-title.xml, for example. That's roughly the way it is.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy2f1y2h5d2vpzxjzdhjhc3m=/font/5a6l5l2t/fontsize/400/fill/ I0jbqkfcma==/dissolve/70/gravity/center ">

The above analysis clarifies the following points:

1. The interface that the activity presents is actually a Phonewindow class in the management. One of the Decorview members in this class is the outermost container.

2. This diagram above is important to show the structure of the form.

3. What is the activity exactly? It's not good to be clear ... ^_^ in short, and the beginning of the understanding is different.

4. See the source code if you don't know the problem. Code is the best teacher!









Process Analysis for Android view loading into a form

Related Article

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.