Android uses addView to dynamically Add a View component to the Activity

Source: Internet
Author: User

Android uses addView to dynamically Add a View component to the Activity

This article describes how to dynamically add layout and controls to the UI interface. During programming, some content needs to be dynamically displayed. When dynamically adding views, the addView method is used.

1. addView method Overview

In Android, you can use the addView function of the Layout View to add dynamically generated View objects to the Layout View.

Example:

Interface code:

 

 

 

Activity Code:

 

Public class helloWorld extends Activity {public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // get the 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 from LinearLayout. removeView (b1 );}}

 

The positions of the above Code are arranged vertically, because the orientation of the Linerlayout interface code is set to vertical, but to be beautiful, you need to set the positions and styles of the added View. There are two types of View to be added: Layout (such as Linearlayout) and control (such as Button and TextView .)

2. dynamically add layout (including style and position)

The following example describes how to dynamically Add a layout. The basic content is consistent with the above Code, and focuses on how to control the location of the layout to be added. Use the LayoutParam class to control the layout position.

Example:

The interface code is similar to the preceding interface code.

Activity Code:

 

RelativeLayout rl = new RelativeLayout (this); // you can specify the width and height of the RelativeLayout layout. layoutParams relLayoutParams = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); this. addView (rl, relLayoutParams );

 

 

3. dynamically add controls

The dynamic addition of controls is similar to the addition of layout. The following code mainly focuses on the Control control position. The following code is used to add a layout supplement to the second item, and the control is added to the newly added layout.

The interface code is not repeated.

Activity Code:

 

RelativeLayout rl = new RelativeLayout (this); // you can specify the width and height of the RelativeLayout layout. layoutParams relLayoutParams = new RelativeLayout. layoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); TextView temp = new TextView (this); temp. setId (1); temp. setText ("image"); rl. addView (temp); TextView TV = new TextView (this); TV. setText ("text"); TV. setId (2); LayoutParams param1 = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); param1.addRule (RelativeLayout. BELOW, 1); // This control is rl under the control with id 1. addView (TV, param1); 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 on the right side of the control with id 1 rl. addView (update, param2); this. addView (rl, relLayoutParams );

 

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

 

 

 

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.