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
- Layoutinflater Inflater = layoutinflater. From (This);
- View view = Inflater. Inflate (R. layout. ID,Null);
- Or simply combine the following statement:
- 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
- Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service );
- 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
-
- Public StaticLayoutinflater from (context ){
-
- Layoutinflater =
- (Layoutinflater) Context. getsystemservice (context. layout_inflater_service );
-
- If(Layoutinflater =Null){
-
- Throw NewAssertionerror ("Layoutinflater not found .");
-
- }
-
- ReturnLayoutinflater;
- }
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
- // Basic usage
-
- Public VoidShowcustomdialog (){
-
- 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_dialog,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 =NewAlertdialog. Builder (mcontext );
- Builder. setview (layout );
-
- Alertdialog = builder. Create ();
-
- Alertdialog. Show ();
-
- }
-
- }
-
-
- Protected VoidShowtoast (IntType ){
-
- Toast. maketext (This,"*********", Toast. length_long). Show ();
-
- Layoutinflater li = (layoutinflater) getsystemservice (context. layout_inflater_service );
-
- View view = Li. Inflate (R. layout. Toast,Null);
-
-
- Toast =NewToast (This);
-
- Toast. setview (View );
- Toast. setduration (type );
-
- Toast. Show ();
-
- }
// 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 ();}