Two ways Android dynamically adds a view to a layout

Source: Internet
Author: User

I. Description

There are two ways to add a view file: 1, by defining LAYOUT;2 in the XML file, writing Java code

Ii. Description of the preface

1. Constructing an XML file

2.LayoutInflater

When it comes to AddView, first look at the Layoutinflater class. The most important feature of this class is the ability to convert the layout of XML representations into view. For the sake of understanding, we can compare it to Findviewbyid (), both of which instantiate an object, and the difference is that Findviewbyid () is instantiated for the specific widget control under the XML layout file, and Layoutinflater is looking for res/ The XML layout file under layout/is instantiated.

(1) Create

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

Layoutinflater inflater = Layoutinflater.from (activity.this);

Layoutinflater inflater = Getlayoutinflater ();

The three methods are essentially the same.

(2) Inflate ()

Use Layoutinflater.inflate () to convert the layout file to view.

View view = Inflater.inflate (R.layout.block_gym_album_list_item, NULL);

3. Add a View file


Third, step
1. By defining layout in an XML file (Block_gym_album_list_item.xml)
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    xmlns:core=" Http://schemas.android.com/apk/res/com.gxtag.gym "    android:layout_width=" Fill_ Parent "    android:layout_height=" fill_parent "    android:orientation=" vertical "    android:padding=" 5DP " >    <imageview        android:id= "@+id/iv_head_album"        android:layout_width= "Wrap_content"        Android : layout_height= "wrap_content"        android:src= "@drawable/defaulthead"/></linearlayout>

2, Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/ll_parent "    android:layout_width=" fill_parent "    android:layout_height=" Wrap_content "    android:orientation=" vertical ">    <include        android:layout_width=" Fill_parent "        android:layout_height= "wrap_content"        layout= "@layout/block_head_back"/></linearlayout>

3, Dynamicviewactivity.xml

Package Com.gxtag.gym.ui;import Android.app.activity;import Android.content.context;import android.os.Bundle; Import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.linearlayout;import Android.widget.linearlayout.layoutparams;import Android.widget.textview;import Com.gxtag.gym.r;import Com.icq.app.widget.statedbutton;public Class Dynamicviewactivity extends Activity implements Onclicklistener{private Context mcontext;private TextView mtv_title; Private String title = "Dynamically add Layout";p rivate statedbutton msbtn_back;private linearlayout mll_parent; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_dynamic ); Mcontext=this;initview (); Mll_parent.addview (AddView1 ()); Mll_parent.addview (AddView2 ());} private void Initview () {//TODO Initialize View mll_parent= (linearlayout) Findviewbyid (r.id.ll_parent); mtv_title = (TextView) Findviewbyid (r.id. Tv_title); Mtv_title.settext (String.Format (String.Format (Getresources () getString (R.string.title), title)));    Msbtn_back = (Statedbutton) Findviewbyid (r.id.sbtn_navback); Msbtn_back.setonclicklistener (this);}                 Private View AddView1 () {//TODO dynamic Add layout (XML mode) linearlayout.layoutparams LP = new Linearlayout.layoutparams ( Layoutparams.fill_parent, layoutparams.wrap_content); Layoutinflater inflater1= (Layoutinflater) Mcontext.getsystemservice (context.layout_inflater_service);// Layoutinflater inflater2 = Getlayoutinflater (); Layoutinflater Inflater3 = Layoutinflater.from (Mcontext); View view = Inflater3.inflate (R.layout.block_gym_album_list_item, null); View.setlayoutparams (LP); return view;}                 Private View AddView2 () {//TODO dynamic Add layout (Java mode) linearlayout.layoutparams LP = new Linearlayout.layoutparams ( Layoutparams.fill_parent, layoutparams.wrap_content);         LinearLayout view = new LinearLayout (this); VIEW.SETLAYOUTPARAMS (LP);//Set layout parameter view.setorientation (LiNearlayout.horizontal);//Set the linearlayout//of the child view to the vertical layout//define the layout of the two elements in the child view viewgroup.layoutparams VLP = NE W viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP         _content);                 Viewgroup.layoutparams VLP2 = new Viewgroup.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT,                  ViewGroup.LayoutParams.WRAP_CONTENT);         TextView TV1 = new TextView (this);         TextView TV2 = new TextView (this);         Tv1.setlayoutparams (VLP);//Set TextView layout tv2.setlayoutparams (VLP2);         Tv1.settext ("Name:");         Tv2.settext ("John Doe"); Tv2.setpadding (CALCULATEDPTOPX (50), 0, 0, 0);//Set Margin View.addview (TV1);//Add TextView to Child view View.addview ( TV2);//Add TextView to the child view in return view;         }private int calculatedptopx (int padding_in_dp) {final float scale = getresources (). Getdisplaymetrics (). density; return (int) (PADDING_IN_DP * scale + 0.5f); } @Overridepublic void OnClick (View v) {//TODO control Click event Switch (V.getid ()) {case r.id.sbtn_navback:this.finish (); Default:break;}}}

4, need to contact me again: qq-508181017












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.