Use of layoutinflater in Android

Source: Internet
Author: User

Inflater indicates expansion. In Android, it indicates expansion.

The role of layoutinflater is similar
Findviewbyid (). The difference is that layoutinflater is used to find and instantiate the XML layout file in the layout folder! While
Findviewbyid () is used to find widgets (such as buttons and textview) in a specific XML file ).

(0) it can be used in many places, such as getview of baseadapter and widget of View in custom dialog.
It can be used in two ways:

Java Code
    1. Layoutinflater Inflater = layoutinflater. From (This);
    2. View view = Inflater. Inflate (R. layout. ID,Null);
    3. Or simply combine the following statement:
    4. View view = layoutinflater. From (This). Inflate (R. layout. ID,Null);
 
Layoutinflater Inflater = layoutinflater. from (this); view = Inflater. inflate (R. layout. ID, null); or simply combine the following statement: view = layoutinflater. from (this ). inflate (R. layout. ID, null );

Another method:
Java code

    1. Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service );
    2. View view = Inflater. Inflate (R. layout. ID,Null);
 
Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service); view = Inflater. Inflate (R. layout. ID, null );

The above two methods are essentially the same. Look at the source code below. Form () calls getsystemservice ():
Java code

  1. Public StaticLayoutinflater from (context ){
  2. Layoutinflater =
  3. (Layoutinflater) Context. getsystemservice (context. layout_inflater_service );
  4. If(Layoutinflater =Null){
  5. Throw NewAssertionerror ("Layoutinflater not found .");
  6. }
  7. ReturnLayoutinflater;
  8. }
 
Public static layoutinflater from (context) {layoutinflater = (layoutinflater) context. getsystemservice (context. layout_inflater_service); If (layoutinflater = NULL) {Throw new assertionerror ("layoutinflater not found. ");} return layoutinflater ;}

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 windowsProgram
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

Java code

  1. // Basic usage
  2. Public VoidShowcustomdialog (){
  3. Alertdialog. Builder;
  4. Alertdialog;
  5. Context mcontext = appactivity.This;
  6. // Either of the following methods can be used
  7. // Layoutinflater Inflater = getlayoutinflater ();
  8. Layoutinflater Inflater = (layoutinflater)
  9. Mcontext. getsystemservice (layout_inflater_service );
  10. View layout = Inflater. Inflate (R. layout. custom_dialog,Null);
  11. Textview text = (textview) layout. findviewbyid (R. Id. Text );
  12. Text. settext ("Hello, welcome to Mr Wei's blog! ");
  13. Imageview image = (imageview) layout. findviewbyid (R. Id. Image );
  14. Image. setimageresource (R. drawable. Icon );
  15. Builder =NewAlertdialog. Builder (mcontext );
  16. Builder. setview (layout );
  17. Alertdialog = builder. Create ();
  18. Alertdialog. Show ();
  19. }
  20. }
  21. Protected VoidShowtoast (IntType ){
  22. Toast. maketext (This,"*********", Toast. length_long). Show ();
  23. Layoutinflater li = (layoutinflater) getsystemservice (context. layout_inflater_service );
  24. View view = Li. Inflate (R. layout. Toast,Null);
  25. Toast =NewToast (This);
  26. Toast. setview (View );
  27. Toast. setduration (type );
  28. Toast. Show ();
  29. }
// Basic usage: Public void showcustomdialog () {alertdialog. builder; alertdialog; Context mcontext = appactivity. this; // either of the following methods can be used // layoutinflater Inflater = getlayoutinflater (); layoutinflater Inflater = (layoutinflater) mcontext. getsystemservice (layout_inflater_service ); View layout = Inflater. Inflate (R. layout. custom_diater, null); textview text = (textview)Layout. Findviewbyid (R. Id. Text); text. settext ("Hello, welcome to Mr Wei's blog! "); Imageview image = (imageview)Layout. Findviewbyid (R. Id. Image); image. setimageresource (R. drawable. Icon ); Builder = new alertdialog. builder (mcontext); builder. setview (layout); alertdialog = builder. create (); alertdialog. show () ;}} protected void showtoast (INT type) {toast. maketext (this, "**********", toast. length_long ). show (); layoutinflater li = (layoutinflater) getsystemservice (context. layout_inflater_service); view = Li. inflate (R. layout. toast, null); Toast = new toast (this); toast. setview (View); toast. setduration (type); toast. show ();}
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.