Android uses AddView to add components dynamically

Source: Internet
Author: User

In project development, we often need to dynamically add components, where there are two items to add: layouts and Components

Where the added layout is mainly relativelayout (relative layout) and LinearLayout (linear layout)

The components you add are mainly text boxes, edit boxes, buttons, and so on.


Below, let's implement:

First we create a new project, delete the code that is not in Mainactivity.class, leaving only the protected void OnCreate (Bundle savedinstancestate) function

To add a new component to the layout file:

1. Introduction to the AddView method

In Android, AddView (ViewGroup view, index) adds a view at the specified index. You can use the AddView function of the layout view to add dynamically generated view objects to the layout view.

2. Example:

(1) First we add a component to the layout file, such as a text, two buttons, at which point we need to add a layout item <linearlayout> in the layout file, and define its ID as linearlay_1, which is used to identify when the component is added. The layout file code looks like this:

    <textview        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text = "Dynamic Add Component Sample"         android:id= "@+id/textview"/>    <linearlayout         android:layout_below= "@+id/textview"        android:id= "@+id/linearlay_1"        android:layout_height= "wrap_content"        android:layout_width= "Wrap_ Content "        android:orientation=" vertical "        ></LinearLayout>
Then we add the components inside the activity class, and the code looks like this:

     /**
* The location of the layout in the code, is arranged vertically 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, split the
* For two classes, one is layout (for example: LinearLayout and Relativelayout, for relativelayout is relative layout)

* Note that for linearlayout layouts, setting landscape or portrait is a must! Otherwise you will not see the effect.

*/

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Bind layout items in activity_main layout file, where R.id.lenearlay_ 1 idlinearlayout linear= (linearlayout) Findviewbyid (r.id.linearlay_1) set in the layout file;//Add text, <span style= "font-family: Arial, Helvetica, Sans-serif; >this represents the current project </span>textview tv=new TextView (This), Tv.settext ("Sample text Box"), Tv.setid (1);//set ID, optional, You can also add a string to the R file and use Linear.addview (TV) as a reference here.     Add the button to the LinearLayout      button B1 = New button (this);      B1.settext ("Cancel");      Linear. AddView (B1);       Add button 2 to linearlayout      button b2 = New button (this);      B2.settext ("OK");      Linear. AddView (B2);       Remove button 1     //Linear from LinearLayout. Removeview (B1);       }}

The effect is as follows:


Figure 1 Adding components dynamically-linearlayout



(2) Adding layouts dynamically:

* 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.
* Note: The layout and controls are used the same way when controlling the position and style.
*/This is only done in mainactivity, not the layout file (. xml), whose code is as follows:

public class Mainactivity extends Activity {protected void OnCreate (Bundle savedinstancestate) {super.oncreate (      Savedinstancestate); Setcontentview (R.layout.activity_main); Create a relative layout relativerelativelayout relative = new Relativelayout (this);      Relative.setbackgroundcolor (Color.yellow);//Add Button1 to relativelayout button btn_r1 = New button (this);      Btn_r1.settext ("Cancel");//Set the displayed character Btn_r1.setid (24);            Relative.addview (BTN_R1);      Add Button2 to relativelayout button btn_r2 = New button (this);      Btn_r2.settext ("OK");//Set the displayed character Btn_r2.setid (25);                                                                                                                                                                   Relative.addview (BTN_R2); Set the width height of the relativelayout layout relativelayout.layoutp        Arams lp=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content); Lp.addrule (ReLativelayout.align_parent_left, relativelayout.true);         Lp.addrule (Relativelayout.align_parent_top, relativelayout.true);   BTN_R1.SETLAYOUTPARAMS (LP);        Sets the layout properties of the button lp=new Relativelayout.layoutparams (layoutparams.wrap_content,layoutparams.wrap_content);         Lp.addrule (relativelayout.right_of, Btn_r1.getid ());   BTN_R2.SETLAYOUTPARAMS (LP); Sets the layout property of the button Setcontentview (relative);}}

The effect is as follows:


Figure 2 Adding a layout dynamically-relativelayout

Learn the above introduction, you can easily layout interface, whether it is a button or other components, for the layout, you can also very convenient layout use, the above is how to dynamically add components in Android method

Android uses AddView to add components dynamically

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.