Inflater indicates expansion. In Android, it indicates expansion.Layoutinflater is similar to findviewbyid (). The difference is that layoutinflater is used to find layout.The XML layout file under the folder and instantiate it! Findviewbyid () is used to find the specific XMLWidgets (such as buttons and textview ).
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 );
Another method:
Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_ Service );
View view = Inflater. Inflate (R. layout. ID, null );
The above two methods are essentially the same. Look at the source code below,Form () calls getsystemservice ():
9 JavaCode
10 public static layoutinflater from (context ){
11 layoutinflater =(Layoutinflater) Context. getsystemservice (context. layout_inflaTer_service );
13 if (layoutinflater = NULL ){
14 throw new assertionerror ("layoutinflater not found .");
15}
16 return layoutinflater;
17}
In addition, getsystemservice () is an important API for Android. It is a method of activity.Input name to get the corresponding object, and then convert it to the corresponding service object. The following describes the system services.
Description of objects returned by input name
Window opened by window_service windowmanager ManagementProgram
Layout_inflater_service layoutinflater gets the view defined in XML
Activity_service activitymanager manages the system status of an application
Power_service powermanger Power Supply Service
Alarm_service alarmmanager alarm clock service
Icationication_service icationicationmanager service in the status bar
Keyguard_service keyguardmanager key lock Service
Location_service the service at the location of locationmanager, such as GPS
Search_service searchmanager
Vebrator_service vebrator mobile phone vibration Service
Connectivity_service connectivity Network Connection Service
Wifi_service wifimanager Wi-Fi Service
Telephony_service teleponymanager telephone service
18 Java code
19 // basic usage
20 public void showcustomdialog (){
21 alertdialog. Builder;
22 alertdialog;
23 context mcontext = appactivity. This;
24 // either of the following methods can be used
25 // layoutinflater Inflater = getlayoutinflater ();
26 layoutinflater Inflater = (layoutinflater)
27 mcontext. getsystemservice (layout_inflater_service );
28 view layout = Inflater. Inflate (R. layout. custom_diater, null );
29 textview text = (textview) layout. findviewbyid (R. Id. Text );
30 text. settext ("Hello, welcome to Mr Wei's blog! ");
31 imageview image = (imageview) layout. findviewbyid (R. Id. Image );
32 image. setimageresource (R. drawable. Icon );
33 builder = new alertdialog. Builder (mcontext );
34 builder. setview (layout );
35 alertdialog = builder. Create ();
36 alertdialog. Show ();
37}
38}
39
40 protected void showtoast (INT type ){
41 toast. maketext (this, "**********", Toast. length_long). Show ();
42
43 layoutinflater li = (layoutinflater) getsystemservice (context. layou
T_inflater_service );
44 view = Li. Inflate (R. layout. Toast, null );
45
46 Toast = new toast (this );
47 toast. setview (View );
48 toast. setduration (type );
49 toast. Show ();
50}