Android dynamic control generation

Source: Internet
Author: User
It is easy to dynamically generate interfaces.
First, you need to configure the layout of this interface, including absolutelayout, relativelayout, and others. Then you can add the control in it:
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Determine the page layout
Absolutelayout abslayout = new absolutelayout (this );
Setcontentview (abslayout );
// Create a button
Button btn1 = new button (this );
Btn1.settext (\ "this is a button \");
Btn1.setid (1 );
// Determine the widget size and position
Absolutelayout. layoutparams lp1 =
New absolutelayout. layoutparams (
Viewgroup. layoutparams. wrap_content,
Viewgroup. layoutparams. wrap_content,
0,100 );
Abslayout. addview (btn1, lp1 );

}
One layout can be arranged on one interface, and multiple la s can be designed together:
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );

// Set the page layout
Relativelayout = new relativelayout (this );
Setcontentview (relativelayout );

// Add an absolutelayout sub-layout and add a button to the layout
Absolutelayout abslayout = new absolutelayout (this );
Abslayout. setid (11 );
Button btn1 = new button (this );
Btn1.settext (\ "This Is A abslayout button \");
Btn1.setid (1 );
Absolutelayout. layoutparams lp0 = new absolutelayout. layoutparams (viewgroup. layoutparams. wrap_content,
Viewgroup. layoutparams. wrap_content, 100,0 );
Abslayout. addview (btn1, lp0 );
// Add this sub-layout to the main layout
Relativelayout. layoutparams lp1 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content );
Lp1.addrule (relativelayout. align_parent_top );
Lp1.addrule (relativelayout. center_horizontal, relativelayout. True );
Relativelayout. addview (abslayout, lp1 );

// Add another sub-Layout
Relativelayout relativelayout1 = new relativelayout (this );
Button btn2 = new button (this );
Btn2.settext (\ "This Is A relativelayout1 button \");
Btn2.setid (2 );
Relativelayout. layoutparams lp2 = new relativelayout. layoutparams (viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. wrap_content );
Lp2.addrule (relativelayout. align_parent_top );
Lp2.addrule (relativelayout. center_horizontal, relativelayout. True );
Relativelayout1.addview (btn2, lp2 );

// Add the layout to the main layout
Relativelayout. layoutparams lp11 = new relativelayout. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content );
Lp11.addrule (relativelayout. below, 11 );
Relativelayout. addview (relativelayout1, lp11 );
}

Related Article

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.