Use of the Android Layoutinflater and inflate () methods (reproduced)

Source: Internet
Author: User
<span id="Label3"></p><p><p>Original Source: http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html</p></p><p><p>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.</p></p>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.).  public View Inflate (int Resourece,viewgrouproot)   Function: Populates a new view hierarchy from the specified XML resource file   <br>The ID of the Resource:view layout<br>Root: rooted view of the resulting hierarchy<br>Return fills the root view of the Hierarchy. If the parameter root is provided, then root is the root view, otherwise the root of the populated XML file is the root view.<p><p>Use of the Android Layoutinflater and inflate () methods (reproduced)</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.