Android and androidsdk

Source: Internet
Author: User

Android and androidsdk

In actual development, the LayoutInflater class is very useful, and its function is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files under res/layout/, while findViewById () is to find the specific widget controls (such as Button and TextView) under the xml layout file ).
Specific functions:
1. LayoutInflater. inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded;

2. For a loaded interface, you can use the Activiyt. findViewById () method to obtain the interface elements.


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

Public abstract class LayoutInflater extends Object

 

Three methods for obtaining a LayoutInflater instance

1.LayoutInflater inflater = getLayoutInflater (); // call the getLayoutInflater () of the Activity ()

2.LayoutInflater localinflater = (LayoutInflater) context. getSystemService

 (Context. LAYOUT_INFLATER_SERVICE );

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

 

In fact, these three methods are essentially the same, as can be seen from the source code:

GetLayoutInflater ():

The getLayoutInflater () method of the Activity is to call the getLayoutInflater () method of PhoneWindow. Let's take a look at the source code:

 

We can see that it actually calls LayoutInflater. from (context ).

 

LayoutInflater. from (context ):

  • Public StaticLayoutInflater from (Context context ){
  • LayoutInflater =
  • (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
  • If(LayoutInflater =Null){
  • Throw NewAssertionError ("LayoutInflater not found .");
  • }
  • ReturnLayoutInflater;
  • }

We can see that it actually calls context. getSystemService ().

 

Conclusion: The three methods are called Context. getSystemService ().

 

Inflate Method
Through the sdk api documentation, you can know that this method has the following overload forms, and the returned values are View objects, as follows:

 

Sample Code:

 

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


Note:

· The inflate method is different from the findViewById method;

· Inflater is used to find and instantiate xml layout files under res/layout;

· FindViewById () is used to find widgets (such as buttons and TextView) in a specific xml layout file ).

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.