It is still very useful to layoutinflater this class in real-world development, which acts like Findviewbyid (). The difference is that Layoutinflater is used to find the XML layout file under res/layout/and is instantiated, while Findviewbyid () is looking for specific widget controls (such as button, TextView, and so on) under the XML layout file.
Specific role:
1, for a non-loaded or want to dynamically load the interface, you need to use Layoutinflater.inflate () to load;
2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to obtain the interface elements.
Layoutinflater is an abstract class that is declared in the document as follows:
Public abstract class Layoutinflater extends Object
Three ways to get Layoutinflater instances
1. Layoutinflater inflater = Getlayoutinflater (); // 2. Layoutinflater inflater = layoutinflater.from (context); 3. Layoutinflater Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
In fact, these three kinds of ways are essentially the same, from the source can be seen:
Getlayoutinflater ():
The Getlayoutinflater () method of the Activity is to call Phonewindow's Getlayoutinflater () method and take a look at the source code:
Public Phonewindow (Context context) { Super(context); = Layoutinflater.from (context);}
It can be seen that it is actually called layoutinflater.from (context).
Layoutinflater.from (context):
Public Static Layoutinflater from (context context) { = (layoutinflater) Context.getsystemservice (context.layout_ Inflater_service); if NULL { thrownew assertionerror ("Layoutinflater not found.") ); } return Layoutinflater;}
It can be seen that it actually calls Context.getsystemservice ().
Conclusion: So the final essence of these three methods is all called Context.getsystemservice ().
Another Getsystemservice () is an important API for Android, a method of activity that obtains the corresponding object according to the name passed in, and then translates it into the corresponding service object.
The following describes the system's corresponding services.
The name passed in |
The returned object |
Description |
Window_service |
WindowManager |
To manage open Windows programs |
Layout_inflater_service |
Layoutinflater |
Gets the view defined in the XML |
Activity_service |
Activitymanager |
Managing the system state of an application |
Power_service |
Powermanger |
Service of power supply |
Alarm_service |
Alarmmanager |
Services for the alarm clock |
Notification_service |
Notificationmanager |
Service for status bar |
Keyguard_service |
Keyguardmanager |
Service for keyboard lock |
Location_service |
Locationmanager |
Location of the service, such as GPS |
Search_service |
Searchmanager |
Search for services |
Vebrator_service |
Vebrator |
Mobile phone Shake Service |
Connectivity_service |
Connectivity |
Network-connected Services |
Wifi_service |
Wifimanager |
Wi-Fi Service |
Telephony_service |
Teleponymanager |
Telephone service |
Inflate method
Through the SDK API documentation, you can know that the method has the following overload forms, the return value is the View object, as follows:
Public View Inflate (int public View Inflate (xmlpullparser parser, ViewGroup root) publicboolean attachtoroot) public View inflate (int Boolean attachtoroot)
Schematic code:
Layoutinflater inflater = (layoutinflater) Getsystemservice (layout_inflater_service); = Inflater.inflate (r.layout.custom, (ViewGroup) Findviewbyid (r.id.test)); // EditText EditText = (EditText) Findviewbyid (r.id.content); // EditText EditText = (EditText) View.findviewbyid (r.id.content);
For the above code, the second parameter ViewGroup root is specified, and of course you can set the null value.
Attention:
Inflate method is different from Findviewbyid method;
Inflater is used to find the XML layout file under Res/layout, and instantiate it;
Findviewbyid () is to find specific widget controls (such as Button, TextView, and so on) in the specific XML layout file.
< turn > Android layoutinflater detailed