I. Description
There are two ways to add a view file: 1, by defining LAYOUT;2 in XML file, Java code writing
Second, the preface notes
1. construct XML file
2.LayoutInflater
Refer to AddView, first of all to understand the Layoutinflater class. The main function of this class is to implement the function of translating the layout of XML representations into view. To make it easier to understand, we can compare it to Findviewbyid (), both of which instantiate an object, and the Findviewbyid () is to find the specific widget control instantiation under the XML layout file, and Layoutinflater find 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 ();
These three methods are essentially the same.
(2) Inflate ()
Use Layoutinflater.inflate () to convert the layout file into view.
View view = Inflater.inflate (R.layout.block_gym_album_list_item, NULL);
3. Add View File
Third, step
1, through the XML file definition layout (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" >
</imageview></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" ></include></linearlayout>
3, Dynamicviewactivity.xml
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{the private context mcontext;
Private TextView Mtv_title;
Private String title = "Dynamically add layout";
Private Statedbutton Msbtn_back;
Private LinearLayout mll_parent;
@Override protected 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 dynamically add layout (XML way) linearlayout.layoutparams LP = new Linearlayou
T.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 dynamically add layout (Java way) linearlayout.layoutparams LP = new Linearlayou
T.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//for the child view as the vertical layout Defines the layout of two elements in a child view viewgroup.layoutparams VLP = new Viewgroup.layoutparams (viewgroup.la
Youtparams.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 ("Dick"); Tv2.setpadding (CALCULATEDPTOPX (50), 0, 0, 0);/set margin View.addview (TV1);//Add TextView to Child view View.addvie
W (TV2);//Add TextView to Child view return view; private int calculatedptopx (int padding_in_dp) {final float scale = getresources (). Getdisplaymetrics (). de
nsity;
return (int) (PADDING_IN_DP * scale + 0.5f); @Override public void OnClick (View v) {//TODO control Click event Switch (V.getid ()) {CAs
e r.id.sbtn_navback:this.finish ();
Break
Default:break; }
}
}