Two ways to add view dynamically in Android

Source: Internet
Author: User

First, the way to use XML:

1, Layoutinflater:

This class parses the layout of the XML representation into a view so that the AddView () method can be added to the view.

2, the difference between Layoutinflater and Findviewbyid:

Both instantiate an object, but the Findviewbyid is instantiated by looking for a specific widget control under the XML layout file, and Layoutinflater is looking for an XML layout file under Res/layout.

3. How to use:

Layoutinflater inflater= (Layoutinflater) Context.getsystemservice (Context.layout_inflater_service);

Layoutinflater Inflater=layoutinflater.from (activity.this);

Layoutinflater Inflater=getlayoutinflater ();

These three ways are essentially the same.

4, Inflate ():

Use Inflate () to parse the XML into a view

Inflaer.inflate (r.layout.xml filename, parent);

Example:

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Wrap_content"3 Android:layout_height= "Wrap_content"4 android:orientation= "vertical" >5 6     <LinearLayout7         Android:id= "@+id/container"8 Android:layout_width= "Wrap_content"9 Android:layout_height= "Wrap_content" >Ten     </LinearLayout> One  A </LinearLayout>
Main.xml
1<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"2Android:layout_width= "Wrap_content"3Android:layout_height= "Wrap_content"4Android:orientation= "vertical" > 5 Span style= "color: #008080;" > 6 <textview 7 android:layout_width= "wrap_content"  8  Android:layout_height= "wrap_content"  9  Android:text= "Hello word" />10 11 </>          
View.xml
1  Public classMainextendsActivity {2 3 @Override4  protected voidonCreate (Bundle savedinstancestate) {5   Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.main);7 addView1 ();8 addView2 ();9 addView3 ();Ten addView4 (); One  } A  - Private voidAddView1 () { -LinearLayout container =(LinearLayout) Findviewbyid (r.id.container); theLayoutinflater Inflater = This. Getlayoutinflater (); -LinearLayout view =(linearlayout) Inflater -. Inflate (R.layout.view,NULL); - Container.addview (view); +  } -  + Private voidaddView2 () { ALinearLayout container =(LinearLayout) Findviewbyid (r.id.container); atLayoutinflater Inflater = Layoutinflater.from ( This); -LinearLayout view =(linearlayout) Inflater -. Inflate (R.layout.view,NULL); - Container.addview (view); -  } -  in Private voidaddView3 () { -LinearLayout container =(LinearLayout) Findviewbyid (r.id.container); toLayoutinflater Inflater = (layoutinflater) This + . Getsystemservice (context.layout_inflater_service); -LinearLayout view =(linearlayout) Inflater the. Inflate (R.layout.view,NULL); * Container.addview (view); $  }Panax Notoginseng  - Private voidaddView4 () { theLinearLayout container =(LinearLayout) Findviewbyid (r.id.container); +LinearLayout view = This. Getlayoutinflater (). Inflate (R.layout.view, A container); the  } +  -}
Main.java

The four AddView () methods are essentially the same.

Second, the way to use Java:

1 Private voidAddviewbyjava () {2LinearLayout container =NewLinearLayout ( This);//Main layout Container3TextView TV =NewTextView ( This);//Sub View TextView4     //Set layout parameters for the main layout container5Linearlayout.layoutparams LLP =NewLinearlayout.layoutparams (6 layoutparams.fill_parent, layoutparams.wrap_content);7Container.setlayoutparams (LLP);//set the layout of container8Container.setorientation (linearlayout.horizontal);//set the orientation of the main layout9     //Set layout parameters for child viewTenViewgroup.layoutparams VLP =NewViewgroup.layoutparams ( One ViewGroup.LayoutParams.WRAP_CONTENT, A ViewGroup.LayoutParams.WRAP_CONTENT); -Tv.setlayoutparams (VLP);//set the layout of TextView -Tv.settext ("Hello word"); theContainer.addview (TV);//add TextView to Container -}
View Code

Two ways to add view dynamically in Android

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.