Android Layoutinflater Detailed

Source: Internet
Author: User

Android Layoutinflater Detailed
Brief introduction:
LayoutInflater This class is still very useful in real-world development, and it works like findViewById() .
The difference is that it is
LayoutInflater used to find res/layout/ the xml layout file and instantiate it, but to findViewById() find the xml specific controls under the layout file widget (such as Button,textview, etc.).
Usage scenarios:
① for an interface that is not loaded or wants to be loaded dynamically, it needs LayoutInflater.inflater() to be loaded.
② for an already loaded interface, you can use Activity.findViewById() methods to get the interface elements in it.
There LayoutInflater are three ways to get an instance:
//Mode 1
//Call activity's Getlayoutinflater ()
Layoutinflater inflater = Getlayoutinflater ();
//Mode 2
Layoutinflater Inflater = layoutinflater.from (context);
//Mode 3
layoutinflater inflater = layoutinflater) context.getsystemservice (Context.layout_inflater_service);
In fact, these three kinds of methods are essentially the same, from the source code can be seen:

ActivityMethod getLayoutInflater() is called by the PhoneWindow method getLayoutInflater() , the source code of the method is:

publicPhoneWindow(Context context){       super(context);       mLayoutInflater = LayoutInflater.from(context);}

You can see that actually the call is LayoutInflater.from(context) , and for the most LayoutInflater.from(context) part, it actually calls, see the context.getSystemService() following source code:

publicstaticfrom(Context context){    LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService     (Context.LAYOUT_INFLATER_SERVICE);    ifnull){            thrownew AssertionError("LayoutInflater not found.");       }       return LayoutInflater;}

Conclusion: The essence of these three methods is to call Context.getSystemService() this method.

getSystemService()is a very important API in Android, it is a method of activity, according to the name of the incoming object, and then converted to the corresponding service object. The following describes the system corresponding services.

the name passed in the returned object Description
Window_service WindowManager To manage open Windows programs
Layout_inflater_service Layoutinflater Gets the view defined in the XML
Activity_service Activitymanager Managing the system state of an application
Power_service Powermanger Service of power supply
Alarm_service Alarmmanager Services for the alarm clock
Notification_service Notificationmanager Service for status bar
Keyguard_service Keyguardmanager Service for keyboard lock
Location_service Locationmanager Location of the service, such as GPS
Search_service Searchmanager Search for services
Vebrator_service Vebrator Mobile phone Shake Service
Connectivity_service Connectivity Network-connected Services
Wifi_service Wifimanager Wi-Fi Service
Telephony_service Teleponymanager Telephone service

The sample code is as follows:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       EditText editText = (EditText)view.findViewById(R.id.content);// 注意:// ·inflate 方法与 findViewById 方法不同;// ·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;// ·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。

Android Layoutinflater Detailed

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.