Detailed explanation and comparison of Android getviewbyid and getlayoutinflator(). Inflate()

Detailed explanation and comparison of Android getviewbyid and getlayoutinflator(). Inflate()

Since I have just learned how to use the methods of getviewbyid and getlayoutinflater(). Inflate() in Android, I don't know how to distinguish them. Here, I'll check the information online and sort it out. You can have a look.

LayoutInflater

To understand this problem, you first need to know what layoutinflator is. According to the official API of Android:

Instantiates a layout XML file into its corresponding View objects.

Therefore, this class is used to instantiate the corresponding view according to the XML layout file.

getLayoutInflater()

According to the API documentation, the definition is followed by the following sentence:

It is never used directly.

It can be seen from this that layoutinflator cannot be used directly, that is, it cannot be initialized with new. At the same time, according to the definition, it can be seen that the layoutinflator class is very useful in actual development. For example, for an interface that has not been loaded or wants to be loaded dynamically, layoutinflator. Inflate () needs to be loaded.

Since it is very useful and cannot be initialized directly, there must be other ways to obtain the instance of layoutinflator. Generally, there are three ways to obtain instances of layoutinflator:

If you look at the source code, the three methods are essentially the same:

Getlayouteinflator () method of activity is to call getlayouteinflator () method of phonewindow.

It can be seen that it is actually calling layoutinflator. From (context).

You can see that it actually calls context. Getsystemservice ().

Conclusion: the final essence of these three methods is to call context. Getsystemservice ().

inflate()

The inflate method can be seen from the SDK API document:

Inflate a new view hierarchy from the specified xml resource.

That is, the inflator () is used to find the XML layout file and instantiate it into a view object.

getViewById()

Getviewbyid () should be familiar with it. It must be one of the first methods I came into contact with when I first came into contact with Android. Findviewbyid () is to find the specific widget control (such as button, textview, etc.) under the XML layout file.

Finally, for an interface that is not loaded or wants to be loaded dynamically, you need to use layoutinflator. Inflate(). Getlayoutinflator() to return the layoutinflator instance. Therefore, it can be said that getlayoutinflator(). Inflator() is used to find and instantiate the XML layout file under RES / layout; Findviewbyid() is to find the specific widget control (such as button, textview, etc.) in the specific XML layout file.

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