Android dynamically adds controls to the layout container. android controls

Source: Internet
Author: User

Android dynamically adds controls to the layout container. android controls

Here, we can use a small demo to dynamically add controls in the layout container. The following uses the dynamic addition of Button controls as an example to add other controls.

1. addView

Add control to layout container

2. removeView

Delete existing controls in the layout container

3. Use it. You will understand it in a small demo.

Public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // generate a LinearLayout, which is used as the layout container to dynamically add three Buttonfinal LinearLayout layout = new LinearLayout (this); layout. setOrientation (LinearLayout. VERTICAL); setContentView (layout); // generate three Buttonfinal buttons btn1 = new Button (this); btn1.setText ("1"); btn1.setText ("Button1 "); final Button btn2 = new Button (this); btn2.setText ("2"); btn2.setText ("Button2"); final Button btn3 = new Button (this ); btn3.setText ("3"); btn3.setText ("Button3"); // dynamically add three buttons to layout. addView (btn1); layout. addView (btn2); layout. addView (btn3); // when you click the button, delete the original layout container layout and add the local container layout. In this way, the added controls are sorted to the end, to understand the idea of dynamically adding controls, btn1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {layout. removeView (btn1); layout. addView (btn1) ;}}); // btn2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {layout. removeView (btn2); layout. addView (btn2) ;}}); // btn3.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {layout. removeView (btn3); layout. addView (btn3) ;}}); setContentView (layout );}}

4,


How to dynamically add controls in relative la s for android Development

The setMargin method is not the RelativeLayout method, but the RelativeLayout. LayoutParams method.
You should use this:
RelativeLayout layout = new RelativeLayout (this );
Layout. setLayoutParams (new RelativeLayout. LayoutParams (-1,-1 ));
TextView mView = new TextView (this );
MView. setId (2 );
MView. setText ("this is a test text! ");
RelativeLayout. LayoutParams layoutParams = new RelativeLayout. LayoutParams (
-2,-2 );
// LayoutParams. setMargins (100,100,100,100 );
LayoutParams. addRule (RelativeLayout. ALIGN_PARENT_BOTTOM,-1 );
Layout. addView (mView, layoutParams );
The preceding example adds a TextView To the bottom of RelativeLayout. You can cancel the comment line, comment the next line, and then see the effect.

Add a sub-layout for android and dynamically add controls for android

This is certainly acceptable. You need to check whether View. inflate () has actually imported XML.
Paste the error message to me.

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.