Two instances of dynamic add view in Android development

Source: Internet
Author: User

For example: To add a button,  view to a linearlayout is a button and the parent view is linearlayout. The properties of the child view are set by Layoutparams. Note that the linearlayout.layoutparams, because the height of the child view, the width of these are for the parent view, to tell the Father view how much space they want to occupy, so it is linearlayout (originally will always use the layoutparams of the child view to set, error)

public class myactivity extends activity {       private  Context mContext;       private LinearLayout mLinearLayout;
       private LinearLayout.LayoutParams mLayoutParams;           @Override        public void  OnCreate (bundle savedinstancestate)  {            Super.oncreate (savedinstancestate);           setcontentview ( R.layout.main);           mContext = this;            mLinearLayout =  (linearlayout) Findviewbyid ( R.id.parent_view);           mlayoutparams = new  linearlayout.layoUtparams (viewgroup.layoutparams.wrap_content, viewgroup.layoutparams.wrap_content);            button button = new button (MContext);            button.settext ("Add button");            mlinearlayout.addview (button, mlayoutparams);        }  }
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"       
           android:orientation= "Vertical"                  android:layout_width= "Fill _parent "                  android:layout_height= "Fill_parent"            >        <LinearLayout                android:id= "@+id/parent_view"                 android:orientation= "Vertical"                 android:layout_width= "Match_parent"                 android:layout_height= "Wrap_content"/>   </LinearLayout> 






Android addview Dynamic to activity Add View component

This article mainly describes how to dynamically add layouts and controls to the UI interface. In the programming time many times need to display some content dynamically, when dynamically adds the view, the main use 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. The

example is as follows:

Activity code:

public class helloworld extends activity {
Public void oncreate (bundle savedinstancestate)  {super.oncreate (savedInstanceState);
Setcontentview ( R.layout.main );
  Obtain 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 (cancellation);
ll. addview  ( b1 );
  Add button 2  to linearlayout  Button b2 = new button (this);
B2.settext (OK);
ll. addview  ( b2 );
  Removal of button 1 ll. removeview  ( b1 ) from linearlayout ; }
}


The position of the above code 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, etc.), one is the control (for example: Button,textview, etc.). )

2. Dynamically adding layouts (including styles and locations)

The following example describes how to dynamically add a layout that is consistent with the code above and focuses on controlling the placement of the added layout. Use the Layoutparam class when controlling the location 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);
Sets the width-height relativelayout.layoutparams rellayoutparams=new relativelayout.layoutparams of the Relativelayout layout
( layoutparams.wrap_content,layoutparams.wrap_content);
This.addview (RL, Rellayoutparams);


3. Dynamically adding controls

Dynamically adding controls and adding layouts are similar, and the following code focuses on controlling the position of the control, adding the following code and the second item to add the layout, adding controls to the newly added layout.

The interface code is also not duplicated.

Activity Class Part code:

relativelayout rl = new relativelayout (this); Sets the width and height relativelayout.layoutparams rellayoutparams=new relativelayout.layoutparams of the relativelayout layout (
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 ("writing");
Tv.setid (2); Layoutparams param1 = new layoutparams (layoutparams.wrap_content,layoutparams.wrap_content
);
Param1.addrule (relativelayout.below, 1);//This control 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
);
Param2.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 that the layout and controls use the same method when controlling the location and style.

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.