57. Add Sub View dynamically (Java/xml two ways)

Source: Internet
Author: User

/*** 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)

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.