A detailed and comparative study of the Android Getviewbyid and Getlayoutinflater () inflate () _android

Source: Internet
Author: User
Tags documentation

A detailed and comparative study of the Android Getviewbyid and Getlayoutinflater (). Inflate ()

because I have just learned how to use Android for Getviewbyid and Getlayoutinflater (), inflate () method of how to do not know how to separate, here on the internet to look up the data, you can see.


The first thing to understand about this question is what is layoutinflater. Explained by the official Android API:

Instantiates a layout XML file into its corresponding View objects.

This class is used to instantiate the corresponding view based on the XML layout file.

Getlayoutinflater ()

According to the API's documentation, the definition follows the following sentence:

It is never used directly.

It can be concluded that Layoutinflater cannot be used directly, that is, it cannot be initialized with new. At the same time, according to the definition can be seen in the actual development of layoutinflater this class is still very useful, such as for a not loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load.

Since it is useful and cannot be directly initialized, there will certainly be other ways of getting the Layoutinflater instance. There are generally three ways to get layoutinflater with examples:

 1. Layoutinflater inflater = Getlayoutinflater (); Invoke Getlayoutinflater () 2 of the activity
 . Layoutinflater Localinflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service);
 3. Layoutinflater Inflater = layoutinflater.from (context);

If you look at the source, in fact, these three ways are essentially the same:

The Getlayoutinflater () method of the activity is to invoke the Getlayoutinflater () method of the Phonewindow.

Public Phonewindow { 
  super (context); 
  Mlayoutinflater = Layoutinflater.from (context); 

You can see that it is actually calling Layoutinflater.from (context).

Layoutinflater.from (context): public

static Layoutinflater from (context context) { 
  Layoutinflater Layoutinflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service); 
  if (Layoutinflater = = null) { 
    thrownew assertionerror ("Layoutinflater not Found."); 
  return layoutinflater; 

It can be seen that it actually calls Context.getsystemservice ().

Conclusion: So the ultimate essence of these three approaches is the invocation of Context.getsystemservice ().

Inflate ()

The inflate method is known through the SDK's API documentation:

Inflate a new view hierarchy from the specified XML resource.

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

Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service); 
View view = Inflater.inflate (R.layout.custom, (ViewGroup) Findviewbyid (r.id.test)); 
EditText EditText = (edittext) View.findviewbyid (r.id.content);

Getviewbyid ()

Getviewbyid () should be familiar with the first contact with Android in a few of the methods must have him. Findviewbyid () is to find the specific widget controls (such as button, TextView, etc.) under the XML layout file.

Finally, for an interface that is not loaded or wants to be loaded dynamically, it is necessary to use Layoutinflater.inflate (), Getlayoutinflater () to return the Layoutinflater instance, so It can be said that Getlayoutinflater (). Inflater () is used to find the XML layout file under Res/layout and instantiate it, Findviewbyid () is to find specific widget controls in specific XML layout files (such as: Button, TextView, etc.).

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

Related Article

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.