Android uses AddView to dynamically add components _android

Source: Internet
Author: User

In project development, we often need to add components dynamically, which can be added in two parts: layout and component

Among them, the added layout mainly has the Relativelayout type (relative layout) and LinearLayout (linear layout)

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

Here, let's do the implementation:

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

1. Introduction to 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, Examples:

(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> to the layout file, and define its ID as linearlay_1 to identify when adding the component. 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 component inside the activity class, and the code looks like this:

   /** * Code, the location of the layout, is in a vertical order because the interface code linerlayout orientation set is * vertical, but for the sake of beauty, you need to set the location and style of the added view. When you add a view, there are two categories to introduce, one is the layout (for example: LinearLayout and Relativelayout, for the relativelayout is a relative layout) * Note that for linearlayout layout, It is necessary to set the landscape or portrait! 
 
  Otherwise, you will not see the effect. * * Public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super
 . OnCreate (Savedinstancestate);
 
 Setcontentview (R.layout.activity_main); Bindings Activity_main Layout items in the layout file, where r.id.lenearlay_1 the ID linearlayout linear= (linearlayout) Findviewbyid set in the layout file (
 R.id.linearlay_1);
 Add text, this represents the current project TextView tv=new TextView (this);
 Tv.settext ("Sample text box");
 
  Tv.setid (1);//set ID, optional, or add a string to the R file, and use the Linear.addview (TV) as a reference here;
  Add a button to the LinearLayout button B1 = New button (this);
  B1.settext ("cancellation"); Linear.
 
  AddView (B1);
  Add button 2 to the LinearLayout button b2 = New button (this);
  B2.settext ("OK"); Linear.
 
  AddView (B2); Remove button 1//Linear from LinearLayout. Removeview (B1 ); 

 }

}

The effect is shown in the following illustration:

Figure 1 Dynamic Add component-linearlayout

(2) dynamic Add layout:

* The following example will describe how to dynamically add a layout that is consistent with the above code and focuses on controlling the placement of the added layout

* Use the Layoutparam class when controlling the location of the layout.

* Note: The layout and controls use the same method when controlling position and style.

* * This time only in the mainactivity operation, does not involve the layout file (. xml), its code is as follows:

 public class Mainactivity extends activity {protected void OnCreate (Bundle savedinstancestate) {super.oncreate (save
 Dinstancestate);
  Setcontentview (R.layout.activity_main);
 Create a relative layout relative relativelayout 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); Sets the width and height relativelayout.layoutparams lp=new relativelayout.layoutparams of the Relativelayout layout (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); Set the layout properties of a 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 properties of the button Setcontentview (relative); 

 }

}

The effect looks like this:

Figure 2 Dynamic Add layout-relativelayout

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

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.