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. Layoutinflater is an abstract class that is declared in the document as follows: Public abstract class Layoutinflater extends object obtains three ways of Layoutinflater 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) { & nbsp 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 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 Shake service Connectivity_serviceConnectivity Network connected service Wifi_service Wifimanager Wi-Fi service Telephony_service Teleponymanager telephony inflate method through the SDK API documentation, You can know that the method has the following overloads, the return value is the View object, as follows: public View inflate (int resource, ViewGroup root) public View Inflate (xmlpullpar Ser parser, ViewGroup root) public View inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) public V Iew Inflate (int resource, ViewGroup root, Boolean attachtoroot) schematic code: Layoutinflater 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 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.).
Transfer from: Link
Android Layoutinflater Detailed (GO)