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 under the XML layout file (such as button, TextView, etc.).
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 a Layoutinflater instance1. 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, 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:
1 Public Phonewindow (Context context) 2 { 3 Super(context); 4 Mlayoutinflater = Layoutinflater.from (context); 5 }
It can be seen that it is actually called layoutinflater.from (context).
Layoutinflater.from (context):
1 Public StaticLayoutinflater from (context context)2 { 3Layoutinflater Layoutinflater =(layoutinflater) Context.getsystemservice4 (context.layout_inflater_service);5 if(Layoutinflater = =NULL)6 { 7 Throw NewAssertionerror ("Layoutinflater not found.")); 8 } 9 returnLayoutinflater;Ten}
It can be seen that it actually calls Context.getsystemservice ().
Conclusion:so the final essence of these three ways is to call the Context.getsystemservice ().
Layoutinflater Summary in Android