In depth analysis and application of Android layoutinflator

Layoutinflator parsing

preface:

In Android, if you are a beginner player, you may not be familiar with layoutinflator. Maybe you have only used it in the oncreateview() of fragment. But people with a little work experience know how important this class is. It is a bridge between layout XML and Java code. We often wonder why Android supports writing layout in XML?

What we think of must be that Android helps us parse XML files internally, and layoutinflator helps us do this work. Firstly, layoutinflator is a system service, which we can see from the from method

Usually, after we get the layoutinflator object, we will call its inflate method to load the layout. Inflate is an overloaded method

You can see that when we call a method with two parameters, it is added to the parent layout by default (the parent layout is generally not empty)

In this method, Resources is used to restore the resource ID to XMlResoourceParser object, then the inflate (XmlPullParser parser, Boolean attachToRoot) method is invoked, and the specific steps to resolve the layout are implemented in this method.

I've annotated the key steps, core

1. Find the root layout label 2. Create the view corresponding to the root node 3. Create its child view

We can see from this that the parsing of the sub view is actually the rinflete method. If there is a root layout in the XML, call createviewfromtag to create the root view in the layout. We can also understand the original of merge, because it directly calls rintlate to add it to the parent view. We can see the difference between the second parameter of rintlate (parser, false) and rintlate (parser, true).

Next, let's look at how rintlate creates multiple layouts

As can be seen from the above, the creation of a view will be handed over to createviewfromtag (view parent, string name, attributeset attributes, Boolean inheritcontext). We can see how this method creates a view

In fact, it is very simple, that is, four downgrades

If we don't set other oncreateview, it will be null. Let's take a look at our oncreateview (). In fact, this method will call createview ()

The general procedure is,

1. Get a specific view constructor from the cache. If not, load the corresponding class and cache the constructor. 2. Use the constructor reflection to construct the corresponding view. 3. If it is a viewstub, copy a layoutinflator object and pass it to it

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>