[Edit] Android layoutinflater

Source: Internet
Author: User

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 Inflater = layoutinflater. From (context );

3.Layoutinflater Inflater = (layoutinflater) Context. getsystemservice

 (Context. layout_inflater_service );


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:

public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}

We can see that it actually calls layoutinflater. From (context ).

 

Layoutinflater. From (context ):

public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}

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

Conclusion: The three methods are called context. getsystemservice ().


In addition, getsystemservice () is an important API for Android. It is a method of activity. It obtains the corresponding object based on the input name and converts it to the corresponding service object. The following describes the system services.

Input name Returned object Description
Window_service Windowmanager Manage open window programs
Layout_inflater_service Layoutinflater Get the view defined in XML
Activity_service Activitymanager Manage the system status of an application
Power_service Powermanger Power Supply Service
Alarm_service Alarmmanager Alarm service
Notification_service Icationicationmanager Status Bar Service
Keyguard_service Keyguardmanager Key lock Service
Location_service Locationmanager Location services, such as GPS
Search_service Searchmanager Search Service
Vebrator_service Vebrator Mobile phone vibration Service
Connectivity_service Connectivity Network Connection Service
Wifi_service Wifimanager Wi-Fi Service
Telephony_service Teleponymanager Telephone Service

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:

 
  1. Public View inflate (INT resource, viewgroup root)
  2. Public View inflate (xmlpullparser parser, viewgroup root)
  3. Public View inflate (xmlpullparser parser, viewgroup root, Boolean attachtoroot)
  4. Public View inflate (INT resource, viewgroup root, Boolean attachtoroot)

Sample Code:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  

View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));

//EditText editText = (EditText)findViewById(R.id.content);// error
EditText editText = (EditText)view.findViewById(R.id.content);
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 ).

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.