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 Activiyt.findviewbyid () method to obtain the interface elements. Note: The inflate method differs from the Findviewbyid method, Inflater is used to find the XML layout file under Res/layout, and is instantiated, and Findviewbyid () is to find specific widget controls in the specific XML layout file ( such as: Button, TextView, etc.). layoutinflater is an abstract class that is declared in the document as follows: Public abstract class Layoutinflater extends object gets Layoutinflater Three ways to instance 1. Layoutinflater inflater = Getlayoutinflater ();//Invoke Activity's Getlayoutinflater () 2. Layoutinflater Inflater = layoutinflater.from (context); 3. Layoutinflater inflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service); in fact, the three kinds of ways are essentially the same, From the source can be seen: Getlayoutinflater (): Activity of the Getlayoutinflater () method is to call Phonewindow Getlayoutinflater () method, look at the source code: public Phonewindow (Context context) { super (context); Mlayoutinflater = 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 Layoutinflater = (layoutinflater) context.getsystemservice (context.layout_inflater_ SERVICE); if (layoutinflater = = null) { throw New 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. name returned object description window_service WindowManager Manage open Windows program Layout_inflater_service layoutinflater Get the system state of the Viewactivity_service Activitymanager management application defined in XML Power_service Powermanger power supply Alarm_service Alarmmanager Alarm Clock service notification_service notificationmanager status bar service Keyguard_servicE Keyguardmanager Keypad Lock service Location_service Locationmanager location services, such as Gpssearch_service Searchmanager Search service Vebrator_ Service Vebrator Phone Vibrate service connectivity_service Connectivity network connected Services Wifi_service Wifimanager Wi-Fi service Telephony_service Teleponymanager Telephone Service inflate method through the API documentation of the SDK, you can know that the method has the following overload forms, the return value is the View object, as follows: public View inflate (int resource, Viewgro Up root) public view inflate (Xmlpullparser parser, ViewGroup root) public View inflate (xmlpullparser parser, Viewgro Up root, Boolean attachtoroot) public View Inflate (int resource, ViewGroup root, Boolean attachtoroot) schematic code: Layoutin Flater 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 above code, specify the second parameter ViewGroup root, and of course you can set it to a null value. Note: The inflate methodUnlike the Findviewbyid method, Inflater is used to find the XML layout file under Res/layout, and is instantiated, and Findviewbyid () is to find specific widget controls in the specific XML layout file (for example: Button, TEXTV Iew, etc.).
Reproduced "Android Layoutinflater detailed"