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. In layman's words, inflate is the equivalent of locating a layout defined in an XML.
Because if you use Findviewbyid () directly in an activity, the component in the layout of Setconentview () is the corresponding one.
So if you use other layout in your activity, such as the layout on the dialog, you also have to set the contents of the Layout on the dialog box (like picture ImageView, Text TextView), you have to use inflate () Find the layout of the dialog box first, and then use the layout object to find the components above it, such as:
View view = View.inflate (this, r.layout.dialog_layout, null);
TextView DIALOGTV = (TextView) View.findviewbyid (R.ID.DIALOG_TV);
Dialogtv.settext ("ABCD");
If the component R.id.dialog_tv is a component on a dialog box, you will definitely get an error with This.findviewbyid (R.ID.DIALOG_TV). Specific effect: 1, for an interface that is not loaded or wants to be loaded dynamically, Need to be loaded using Layoutinflater.inflate (), 2, for an already loaded interface, you can use the Activiyt.findviewbyid () method to get 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) { throw new Assertionerror ("LAYOUTINFL Ater 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 Vibrate service connectivity_service Connectivity Network connection service 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 INFL Ate (int resource, ViewGroup root) public View inflate (Xmlpullparser parser, ViewGroup root) public view inflate (Xml Pullparser parser, ViewGroup root, Boolean attachtoroot) public View 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; Findviewbyid () is looking for specific The specific widget controls in the XML layout file (for example, Button, TextView, and so on).
Android Layoutinflater Detailed