Android uses AddView dynamic to add view components to activity

Source: Internet
Author: User

This article mainly describes how to dynamically add layouts and controls to the UI interface, when programming many times need to dynamically display some content, when dynamically adding view, the main use of the AddView method.

1. Introduction to the AddView method

In Android, you can use the AddView function of the layout view to add dynamically generated view objects to the layout view.

Examples are as follows:

Interface code:

<linearlayout   android:id= "@+id/viewobj"   android:layout_width= "wrap_content"   android:layout_ height= "Wrap_content"   android:orientation= "vertical"   android:layout_x= "1px"   android:layout_y= "1px" />


Activity code:

public class HelloWorld extends Activity {public    void OnCreate (Bundle savedinstancestate) {      super.oncreate ( Savedinstancestate);      Setcontentview (r.layout.main);       Get LinearLayout object      linearlayout ll = (linearlayout) Findviewbyid (r.id.viewobj);       Add TextView to LinearLayout      TextView TV = new TextView (this);      Tv.settext ("Hello World");      LL. AddView (TV);       Add button 1 to linearlayout      button B1 = New button (this);      B1.settext ("Cancel");      LL. AddView (B1);       Add button 2 to linearlayout      button b2 = New button (this);      B2.settext ("OK");      LL. AddView (B2);       Remove button 1      LL. Removeview (B1) from LinearLayout;   }


The position of the code above is arranged in a vertical order because the interface code linerlayout the orientation setting is vertical , but in order to be beautiful, You need to set the location and style of the added view. When adding a view, there are two categories, one is the layout (for example: LinearLayout, etc.), one is the control (for example: Button,textview, etc.). )

2. Dynamically add layouts (including styles and locations)

The following example shows how to add a layout dynamically, with the basic content consistent with the code above, focusing on how to control the location of the added layout. Use the Layoutparam class when controlling the placement of the layout.

Example:

The interface code is similar to the interface code above and is not repeated.

Activity Class Part code:

Relativelayout RL = new Relativelayout (this);//Set the width of the relativelayout layout relativelayout.layoutparams rellayoutparams=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content); This.addView (RL, Rellayoutparams);


3. Adding controls Dynamically

Adding controls dynamically and adding layouts are similar, the following code focuses on the position of the control control, the following code and the second item adds a layout supplement, and then adds the control to the newly added layout.

The interface code is also not duplicated.

Activity Class Part code:

Relativelayout RL = new Relativelayout (this);//Set the width of the relativelayout layout relativelayout.layoutparams rellayoutparams=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content); TextView temp = new TextView (this), temp. SetId (1); Temp.settext ("picture"); Rl.addview (temp); TextView TV = new TextView (this), Tv.settext ("text"); Tv.setid (2); Layoutparams param1 = new Layoutparams (layoutparams.wrap_content,layoutparams.wrap_content);p Aram1.addrule ( Relativelayout.below, 1);//This control is Rl.addview (TV,PARAM1) below the control with ID 1; Button update = New button (this); Update.settext ("button"); Layoutparams param2 = new Layoutparams (layoutparams.wrap_content,layoutparams.wrap_content);p Aram2.addrule ( Relativelayout.right_of, 1);//This control is Rl.addview (UPDATE,PARAM2) to the right of the control with ID 1, This.addview (RL, Rellayoutparams);


Note: When you control the position and style, the layout and the control are used the same way.

4. Location parameter Introduction

See URL: http://blog.sina.com.cn/s/blog_5da93c8f0101azq7.html

5. Reference website:

(1) http://blog.csdn.net/frdde/article/details/7289047

(2) http://blog.csdn.net/xanxus46/article/details/7742446

(3) http://blog.sina.com.cn/s/blog_5da93c8f0101azq7.html

(4) http://blog.sina.com.cn/s/blog_4ac60f270100myjk.html

Android uses AddView dynamic to add view components to activity

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.