Deeply analyze the loading layout principle of setcontentview in Android

preface

For Android developers, setcontentview is very familiar. In our activity, we first use it to load our layout, but some people don't know the principle of loading layout, including me. Today, we will analyze the principle of loading layout of setcontentview from the perspective of source code.

preparation

Because part of the source code of the Android API we use is hidden, when we can't find the source code in Android studio, we can download the corresponding source code on the official website to view it. Of course, download the corresponding version of the API in GitHub to replace the android.jar of the corresponding API in platforms under our SDK. In this way, we can see the hidden API in Android studio, and breakpoint debugging can help us read the source code.

The analysis source code of this article is Android 7.1 (api25).

Source code analysis of Activiy setcontentview

In the activity, setcontentview finally calls the setcontentview ・ method of getwindow(). Getwindow() returns a window class, which represents the concept of a window. Our activity is a window. Dialog and toast are also displayed through window, which is easy to understand. It is an abstract class, and the specific implementation is phonewindow, It handles almost all the logic related to loading the layout.

First judge whether mcontentparent is empty. Of course, mcontentparent is empty when it is started for the first time, and then execute installdecor(); method.

If mcontentparent is not empty, judge whether there is transition animation through hasFeature (feature_content_transitions). If not, pass mcontentparent. Removeallviews(); Remove all views under the mcontentparent node. Then fill our layout into mcontentparent through inflate. Finally, the callback of content change. As for what mcontentparent is, let's leave a suspense and talk about it later.

In the above method, the main work is to initialize mdecor and mcontentparent, as well as the initialization of some properties

Generatedecor initializes a decorview object. Decorview inherits FrameLayout and is the top-level view of the layout to be displayed. The layout and title bar we see are all in it.

Then call generatelayout with mdecor as a parameter to initialize mcontetparent

There are many codes. First get the theme style through getwindowstyle for initialization, and then get different features set through getlocalfeatures to load different layouts. For example, we usually add requestwindowfeature (window. Feature_no_title) to activity; To hide the title bar. No matter which layout is ultimately used by the feature, there is a FrameLayout of Android: id = "@ Android: ID / content", and our layout file is added to the FrameLayout. Let's look at a simple layout

Through the above analysis, you should understand why the requestwindowfeature must be set before setcontentview. If it is set later, the features have been read locally during the execution of setcontentview through the above analysis, but it has not been set at this time. Of course, it is invalid.

Get the object through findviewbyid above. However, there is another important method before getting the ViewGroup

It's easy to understand. Root judges the layout loaded according to different features, and then adds the layout to decorview through addview. It's all successful at first

After returning to the first sentence of code in setcontentview, as above, we can easily understand that it is to inflate our layout file into mcontentparent. Here, the loading layout file of activity is completed.

Setcontentview analysis of appcompatactivity

Because the loading layout of setcontentview of appcompatactivity is different from that of activity in many ways, and it is slightly more complex than that of activity, we will also briefly analyze it here.

By name, you know that the loading layout is given to a delegate object.

Appcompatdelegate is an abstract class, as shown in the figure below. It has several subclasses to implement

Why are there so many subclasses? In fact, we can guess from the name. It's for compatibility. To prove this, let's look at the Create method

It is obvious here that different delegates are initialized according to different API versions. By viewing the code, the setcontentview method is implemented in appcompatdelegateimplv9

With the loading experience of analyzing activity, we can easily understand that contentparent and mcontentparent in activity are the same thing. Ensuesubdecor is the initial mcontentparent, and then removeallviews, and then fill our layout into contentparent. Finally, the callback is executed.

The above processing logic is to initialize some theme styles first, then initialize decorview. And layout through mwindow. Getdecorview(), then createsubdecor loads different layout subdecors according to the theme, and obtain contentview (view r.id.action_bar_activity_content in layouts loaded by appcompact according to different themes) and windowcontentview through findviewbyid (view Android. R.id.content in decorview) control. After obtaining the control, clear the ID of windowcontentview and change the ID of contentview from r.id.action_bar_activity_content to android.r.id.content. Finally, add the subdirector to decorview through mwindow.setcontentview (subdirector).

As for the layout of subdicor, let's look at a layout r.layout.abc_ screen_ Toolbar, a layout loaded with a title (mwindownotitle is false) and an actionbar (mhasactionbar is true).

Regardless of the layout under any theme, there will be a layout with ID ABC_ screen_ content_ It is better to change the ID to Android. R, content, and then add it to the mcontentparent in mdecor. We can view our layout hierarchy with the hierarchy viewer tool under tools in the SDK. For example, the layout file passed in by setcontentview in appcompatactivity is a thread layout. If there is a button under the layout, the hierarchy will be viewed

Reference link: http://www.weyye.me/detail/framework-appcompatactivity-setcontentview/

summary

The above is the whole content of this article. Here setcontentview has finished the analysis. I hope the content of this article has a certain reference value for your study or work. Due to the limited level, it is inevitable to make mistakes. If you find something wrong or wrong during reading, leave a message and correct it. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>