Layoutinflater This class is also very useful, and it functions like Findviewbyid (). But he's looking for an XML file and instantiating it.

Source: Internet
Author: User

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, these three ways are essentially the same, as can be seen from the source code: Getlayoutinflater () : The Getlayoutinflater () method of the Activity is to call Phonewindow's Getlayoutinflater () method and take a look at the source code: Public Phonewindow (Context context) {   super (context); &NBsp;     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.   The name passed in Description of the returned objectWindow_servicewindowmanager Managing open Windows programs Layout_inflater_serviceLayoutinflater Gets the view defined in the XML Activity_serviceActivitymanager managing the System state of an application Power_service Service of Powermanger power supply Alarm_serviceAlarmmanager Alarm Clock service Notification_serviceNotificationmanager service for status bar Keyguard_serviceKeyguardmanager Keypad lock service Location_serviceLocationmanager Location of services, such as GPS Search_service Services for Searchmanager search Vebrator_servicevebrator mobile phone Shake service Connectivity_serviceConnectivity Network-connected services Wifi_serviceWifimanager Wi-Fi service Telephony_serviceTeleponymanager 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 inflate (int resource, ViewGroup root) public view Inflate (Xmlpullparser parser, ViewGroup root) public View inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtor oot) public View inflate (int resource, ViewGroup root, Boolean attachtoroot) schematic code: Layoutinflater inflater = (layoutinflate      R) Getsystemservice (Layout_inflater_service);      View view = Inflater.inflate (R.layout.custom, (ViewGroup) Findviewbyid (r.id.test)); EditText EditText = (EditText) Findviewbyid (r.id.content);//Erroredittext 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.).

Layoutinflater This class is also very useful, and it functions like Findviewbyid (). But he's looking for an XML file and instantiating it.

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.