Android LayoutInflater and layoutinflater

Source: Internet
Author: User

Android LayoutInflater and layoutinflater

In actual development, the LayoutInflater class is very useful, and its function is similar to findViewById (). The difference is that LayoutInflater is used to find and instantiate xml layout files under res/layout/, while findViewById () is to find the specific widget controls (such as Button and TextView) under the xml layout file ).

* Specific functions: 1. LayoutInflater. inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded. 2. For a loaded interface, you can use Activity. findViewById () to obtain the interface elements. * Three methods for obtaining a LayoutInflater instance: 1.
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.main, null);

2.

LayoutInflater inflater = LayoutInflater.from(context);View layout = inflater.inflate(R.layout.main, null);

3.

LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.main, null);

These three methods are essentially the same, as can be seen from the source code:

GetLayoutInflater ():

The getLayoutInflater () method of the Activity is to call the getLayoutInflater () method of PhoneWindow. Let's take a look at the source code:

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

It can be seen that it actually calls 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;}

We can see that it actually calls context. getSystemService ().

  

Conclusion: The three methods are called Context. getSystemService ().

In addition, getSystemService () is an important API for Android. It is a method of Activity. It obtains the corresponding Object based on the input NAME and converts it to the corresponding service Object. The following describes the system services. Input Name Returned object DescriptionWINDOW_SERVICE WindowManager manages the Opened Window program metadata to get the view ACTIVITY_SERVICE ActivityManager defined in xml to manage the system status of the application. The service ALARM_SERVICE AlarmManager is powered by POWER_SERVICE PowerManger. The service KEYGUARD_SERVICE KeyguardManager in the status bar. the LOCATION_SERVICE LocationManager service of the key lock, for example, the CONNECTIVITY_SERVICE SearchManager service is searched by GPS SEARCH_SERVICE SearchManager. The Connectivity Network Connection Service is WIFI_SERVICE WifiManager. the Wi-Fi service is TELEPHONY_SERVICE T eleponyManager. * inflate method: Through the sdk api documentation, you can know that this method has the following overload forms, and the returned values are View objects, as follows:
public View inflate (int resource, ViewGroup root) public View inflate (XmlPullParser parser, ViewGroup root)public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  public View inflate (int resource, ViewGroup root, boolean attachToRoot)

Sample Code:

  

//public View inflate (int resource, ViewGroup root) 
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); 

Purpose: Fill in a new view hierarchy from the specified XML resource file.
ReSource: The layout ID of the View.
Root: root View of the generated hierarchy
Return fill the Root View of the hierarchy. If the root parameter is provided, root is the root view; otherwise, the root of the filled XML file is the root view.

For the code above, specify the second parameter ViewGroup root. Of course, you can also set it to null.

The other several overloaded inflate functions are similar.

 

* Differences between setContentView and inflate:

public class MyInflate extends Activity{    private TextView tv;    public void OnCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        //setContentView(R.layout.main);        //tv = (TextView) findViewById(R.id.tv);         LayoutInflater inflate = LayoutInflater.from(this);        View view = inflate.inflate(R.layout.main,null);        setContentView(view);    }}

The Code commented out above is the same as the Code not commented out.

 

**!! **

Once setContentView () is called, layout immediately displays the UI.

Inflate only forms a ready-made object in the view class in Layout. If necessary, it is displayed in setContentView (view.

Generally, the interface is displayed through setContentView () in the activity, but if you want to set the layout of the control in a non-activity, LayoutInflater needs to be dynamically loaded.

  

 

  

 

  

  

 

Related Article

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.