/*** Add child view dynamically (Java)*/Private View Createviewwithjava () {linearlayout.layoutparams LP = new Linearlayout.layoutparams ( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); LinearLayout view = new LinearLayout (this); VIEW.SETLAYOUTPARAMS (LP);//Set Layout parameters View.setorientation (linearlayout.horizontal);//Set the linearlayout//of the child view to vertical layout
Defines the layout of two elements in a child view viewgroup.layoutparams VLP = new Viewgroup.layoutparams (ViewGroup.LayoutParams.WR Ap_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 ("Xiao Ming"); Tv2.setpadding (CALCULATEDPTOPX (50), 0, 0, 0);//Set Margin View.addview (TV1);//Add TextView to Child view View.addview (TV 2);//Add TextView to the child view in return view; }
/** *Dynamically adding child view (XML)*/Private View Createviewwithxml () {linearlayout.layoutparams LP = new Linearlayout.layoutparams ( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); View view = Layoutinflater.from (this). Inflate (r.layout.a1_post_params, NULL);//You can also load layouts from XML
VIEW.SETLAYOUTPARAMS (LP);//Set layout parameters
EditText key = (EditText) View.findviewbyid (R.id.key); EditText value = (EditText) View.findviewbyid (R.id.value);
Add Keys.add (key) to the set of key values; Values.add (value);
return view; }
From for notes (Wiz)
57. Add Sub View dynamically (Java/xml two ways)