Detailed explanation of layoutinflator usage in Android Development

This article illustrates the use of layoutinflator in Android development. Share with you for your reference, as follows:

In the actual development, the layoutinflator class is still very useful. Its function is similar to findviewbyid(). The difference is that layoutinflator 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.) under the XML layout file.

Specific functions:

1. For an interface that is not loaded or wants to be loaded dynamically, you need to use layoutinflator Inflate() to load;

2. For a loaded interface, you can use activiyt The findviewbyid () method to obtain the interface elements.

Layoutinflator is an abstract class, which is declared in the document as follows:

public abstract class LayoutInflater extends Object

There are three ways to get an instance of layoutinflator

1. LayoutInflater inflater = getLayoutInflater();// Call getlayoutinflator() of activity

2. LayoutInflater inflater = LayoutInflater. from(context);

3. LayoutInflater inflater = (LayoutInflater)context. getSystemService (Context.LAYOUT_INFLATER_SERVICE);

In fact, the essence of these three methods is the same, as can be seen from the source code:

getLayoutInflater():

Getlayouteinflator () method of activity is to call getlayouteinflator () method of phonewindow. Take a look at the source code:

You can see that it is actually calling layoutinflator from(context)。

LayoutInflater. from(context):

You can see that it actually calls context getSystemService()。

Conclusion: therefore, the final essence of these three methods is the calling context getSystemService()。

In addition, getsystemservice () is a very important API for Android. It is a method of activity. It obtains the corresponding object according to the passed name, and then converts it into the corresponding service object. The following describes the corresponding services of the system.

Inflate method

Through the SDK API document, you can know that this method has the following overload forms, and the return values are view objects, as follows:

Schematic Code:

For the above code, the second parameter ViewGroup root is specified. Of course, you can also set it to null value.

be careful:

・ the inflate method is different from the findviewbyid method; ・ 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.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic components usage, summary of Android view technology, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

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