Reprint of "Layoutinflater Inflate function usage detailed"

Source: Internet
Author: User

The Layoutinflater function is to instantiate an XML layout file of layout as a view class object.

There are three ways to get Layoutinflater :

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.main, null);LayoutInflater inflater = LayoutInflater.from(context); (该方法实质就是第一种方法,可参考源代码)View layout = inflater.inflate(R.layout.main, null);LayoutInflater inflater = getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)View layout = inflater.inflate(R.layout.main, null);

It's been a little tangled . Setcontentview and inflate the difference to find some information. Wrote a small program looked under:

?
publicclass MyInflate extendsActivity{    privateTextView tv;    publicvoid 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);    }}

Both the commented out code and the code that are not commented out are the same in both cases.

Difference:
Setcontentview () once called , layout will display the UI immediately, and inflate will only form an object that is implemented in the view class, and then , if necessary, Setcontentview (view) is displayed. The interface is typically displayed in activity through setcontentview (), but if the control layout is set to operate in non- activity, this requires layoutinflater dynamic loading.

Public View Inflate (int resourece,viewgroup root)
Role: Populates a new view hierarchy from the specified XML resource file
ReSource: The ID of the layout of the view
Root: Rooted view of the resulting hierarchy
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.

The inflate functions are similar for the remaining several overloads .

Function:  
1, for an interface that is not loaded or wants to be loaded dynamically, it needs to be loaded with inflate.  

2, for an already loaded activity, You can use the Findviewbyid method that implements this activiyt to get the interface elements in it.  

Method:  
   android you want to create a picture , beginner is generally a new class, inherit the activity base class, and then use the Setcontentview method in OnCreate to load a well-defined interface in XML.  

    In fact, the activity inside uses Layoutinflater to load the interface, through getsystemservice (context.layout_inflater_service) The Layoutinflater method can be obtained with a single-pass or layoutinflater inflater = Getlayoutinflater (); To obtain. Then use the Inflate method to load the xml,  of the layout;

The following is a simple example: first we need to know what is already loaded layout, what is not loaded. We start an application, the layout{common to entry activity is Main.xml} is loaded, that is, in OnCreate () The other layout is not loaded. It is necessary to dynamically load or pass another activity. It is very useful to actually develop the kind of layoutinflater, which acts like Findviewbyid (), The difference is that Layoutinflater is used to find layouts under the XML layout file and instantiate it! Findviewbyid () is looking for specific widget controls under specific XML. To make it easy for everyone to understand me [turn] did a simple demo, the main layout main.xml there is a textview and a button, when Clicked Button, appears Dialog, and this Dialog layout is the Custom_dialog.xml file we defined in the layout directory (inside and around, left ImageView, right TextView).

The code is as follows: Package Com.bivin; import Android.app.activity;import Android.app.alertdialog;import Android.content.context;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.textview; public class Mainactivity extends Activity implements Onclicklistener { private Button button; public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main);  button = (Button) Findviewbyid (R.id.button); Button.setonclicklistener (this);}   @Overridepublic void OnClick (View v) { showcustomdialog ();}  public void Showcustomdialog () {Alertdialog.builder Builder; Alertdialog Alertdialog; Context Mcontext = Mainactivity.this;  layoutinflater Inflater = (layoutinflater) mcontext.getsystemservice (Layout_inflater_service); View layout = Inflater.inflate (R.layout.custom_dialog, NULL); TextView Text = (TextView) Layout.findviewbyid (R.id.text); Text.settext ("Hello, Welcome to Mr Wei ' s blog!"); I Mageview image = (ImageView) Layout.findviewbyid (r.id.image); Image.setimageresource (r.drawable.icon); builder = new Alertdialog.builder (Mcontext); Builder.setview (layout); Alertdialog = Builder.create (); Alertdialog.show ();}}

Reprint the Inflate function usage of layoutinflater "

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.