Setcontentview () Getting Started

Source: Internet
Author: User

Basis

The whole process is basically done in Phonewindow. First list the methods that will be used in it. As follows:

    It can be seen that it Mdecor for Decorview object protected Decorview Generatedecor () {return new Decorview (GetContext (),-1);    }//findviewbyid () is as follows: Public View Findviewbyid (@IdRes int id) {return Getdecorview (). Findviewbyid (ID); }//gets the settings for the Window property in theme public final TypedArray Getwindowstyle () {synchronized (this) {if (Mwindow Style = = null) {Mwindowstyle = Mcontext.obtainstyledattributes (Com.android.interna            L.r.styleable.window);        } return Mwindowstyle; }} public final TypedArray obtainstyledattributes (@StyleableRes int[] attrs) {return Gettheme (). Obtainstyle    Dattributes (ATTRS);    Whether the}//has the specified feature.    public boolean hasfeature (int feature) {return (Getfeatures () & (1 << feature))! = 0; }//set feature. The above getfeatures () just returns Mfeatures public boolean requestfeature (int featureid) {final int flag = 1<<featureid        ;     Mfeatures |= Flag;   Mlocalfeatures |= Mcontainer! = null?        (flag&~mcontainer.mfeatures): Flag;    Return (mfeatures&flag)! = 0; }
Phonewindow#setcontentview

Call Activity#setcontentview () will eventually be called to Phonewindow#setcontentview. The code is as follows:

    @Override public    void Setcontentview (int layoutresid) {        if (mcontentparent = = null) {            Installdecor ();        } else if (!hasfeature (feature_content_transitions)) {            mcontentparent.removeallviews ();        }        if (Hasfeature (feature_content_transitions)) {            final Scene newscene = Scene.getsceneforlayout (Mcontentparent, Layoutresid,                    getcontext ());            Transitionto (Newscene);        } else {            mlayoutinflater.inflate (layoutresid, mcontentparent);        }        Final Callback cb = Getcallback ();        if (CB! = null &&!isdestroyed ()) {            cb.oncontentchanged ();}    }
First look at the role of Installdecor: Create a Decorview object and assign a value to Mdecor. Assign a value to the mcontentparent at the same time.
Second, if the feature_content_transitions is not set, then remove all the original components and call Mlayoutinflater.inflate (Layoutresid, mcontentparent); Adds a new layout to the mcontentparent.
If there is a feature_content_transitions, the interface will be switched through the Transitionto, the switching process will have a fade effect.

Finally, the oncontentchanged () callback is called.

The whole logic is so much, the key is to see what mcontentparent refers to-because it is the parent class of the layout we write.

phonewindow#generatelayout (Decorview)

        because Installdecor (generatelayout) is called in Mdecor () Generate Mcontentparent, so look at the Generatelayout (Mdecor) method first.
        Its method looks more, but the essence is to find the corresponding layout according to the theme of the activity. Inflate the layout and returns the ViewGroup with the ID android:id/content.

     Protected ViewGroup generatelayout (Decorview decor) {TypedArray a = Getwindowstyle ();//Apply data from Curre        NT theme.        An output statement that omits//gets the windowisfloating attribute in theme misfloating = A.getboolean (r.styleable.window_windowisfloating, false); int flagstoupdate = (flag_layout_in_screen|        Flag_layout_inset_decor) & (~getforcedwindowflags ());            if (misfloating) {setlayout (wrap_content, wrap_content);        SetFlags (0, flagstoupdate); } else {setflags (flag_layout_in_screen|        Flag_layout_inset_decor, flagstoupdate); }//According to the specified property call Requestfeatures () set some feature and flags, the specific code is slightly if (A.getboolean (R.styleable.window_windownotitle, FAL SE) {requestfeature (feature_no_title);//Untitled FEATURE} else if (A.getboolean (R.STYLEABLE.WINDOW_WINDOWAC        Tionbar, False)) {requestfeature (Feature_action_bar);//Don ' t allow a ACTION BAR if there is no title. }//a bit of code if (A.getbooLean (R.styleable.window_windowswipetodismiss, False)) {requestfeature (Feature_swipe_to_dismiss);//Slide back.        But unfortunately api21 later, and there are bugs}//slightly setflags and Requestfeature code, they are based on a set of some attribute value int layoutresource;        int features = Getlocalfeatures (); Gets the value of layoutresourece based on the features value, which represents the ID of a layout.        The code is slightly mdecor.startchanging ();//Add the layout and add the layout to the decor.        View in = mlayoutinflater.inflate (Layoutresource, NULL);        Decor.addview (in, New Viewgroup.layoutparams (Match_parent, match_parent)); Mcontentroot = (ViewGroup) in;//gets several viewgroup,layoutresource that add its own view, but it will mean a @android:id/content with an ID of ViewGroup,        For example, see the following screen_swipe_dismiss.xml viewgroup contentparent = (viewgroup) Findviewbyid (id_android_content);        Some settings on features mdecor.finishchanging ();    return contentparent; }
The specific explanation is in the comment. Where id_android_contrent is defined as follows:
     /**     * The ID, the main layout in the XML layout file should has.     */public    static final int id_android_content = com.android.internal.r.id.content;

From this note we can also see the role of Contentparent.

Installdecor ()re-analyze the following Installdecor () code:
    private void Installdecor () {if (Mdecor = = null) {//Generate Mdecor, and make a series of settings Mdecor = Generatedecor (); Mdecor.setdescendantfocusability (viewgroup.focus_after_descendants);//When a child view does not get focus, it acquires Focus Mdecor.setis            RootNamespace (TRUE); if (!minvalidatepanelmenuposted && minvalidatepanelmenufeatures! = 0) {mdecor.postonanimation (MINV            alidatepanelmenurunnable);            }} if (mcontentparent = = null) {mcontentparent = Generatelayout (Mdecor);            Set up decor part of the UI to ignore fitssystemwindows if appropriate.            Mdecor.makeoptionalfitssystemwindows (); Mdecor is a viewgroup, omitted here is the code that initializes some of these sub-view settings if (mdecor.getbackground () = = NULL && mbackgroundfall            Backresource! = 0) {mdecor.setbackgroundfallback (Mbackgroundfallbackresource);          }//Only inflate or create a new transitionmanager if the caller hasn ' t  Already set a custom one. Some initialization work on Transitionmanager}}
The main process is divided into two parts: generating Mdecor.

Based on Mdecor, Mcontentparent is generated by Generatelayout (Mdecor). The mcontentparent is then set up in a series of settings.

Summary

Above is the analysis of the whole process:

Initialize the Decorview first, and then load a different layout for decorview depending on the features of the settings.
Then in Decorview find an ID of android:id/content mcontentparent--which is the parent container of its own layout.
Finally, you add your own layout to the mcontentparent through Layoutinflater.inflate.

file

A Decorview-loaded layout:

<com.android.internal.widget.swipedismisslayout    xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @android: id/content "    android:fitssystemwindows=" true "    android:layout_width=" Match_parent "    android:layout_height=" match_parent "    />
As you can see, it does have a viewgroup ID of android:id/content.

Setcontentview () Getting Started

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.