Android Notes (61) adding components dynamically

Source: Internet
Author: User

To have a feature, click the button to add a new set of components to the existing layout.

Dynamically creating components, essentially creating components, is simply created in the program based on logic. The approximate step is to find the location where you want to create the control, and then add the component that will be created.

Look at the code:

Mainactivity.java

 Packagecom.example.test;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.ID.BT); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {linearlayout mlinearlayout=(LinearLayout) Findviewbyid (r.id.widgets); Button BT=NewButton (Getapplicationcontext ()); Bt.settext ("Button"); TextView TV=NewTextView (Getapplicationcontext ()); Tv.settext ("TEXTVIEW");                Mlinearlayout.addview (BT);            Mlinearlayout.addview (TV);    }        }); }}

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:id= "@+id/bt"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "point I add" />    <ScrollViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" >        <LinearLayoutAndroid:id= "@+id/widgets"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical" >        </LinearLayout>    </ScrollView></LinearLayout>

Operation Result:

Layoutinflater

Layoutinflater acts like Findviewbyid (), unlike Layoutinflater, which is used to find the XML file under res/layout/and instantiate it, while Findviewbyid is looking for controls in the XML file.

For an interface that is not loaded or lowered into a dynamic load, layoutinflater.inflate is required to load

For an already loaded interface, use Findviewbyid to get the elements

Modify the above code:

Mainactivity.java

 Packagecom.example.test;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.widget.Button;Importandroid.widget.LinearLayout;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Button Button=(Button) Findviewbyid (R.ID.BT); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {linearlayout mlinearlayout=(LinearLayout) Findviewbyid (r.id.widgets); //com.example.test.MyLinearLayout mylinearlayout = new//Com.example.test.MyLinearLayout (//Getapplicationcontext ()); //Mlinearlayout.addview (mylinearlayout);Layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.widget, mlinearlayout);    }        }); }}

Activity_main.xml

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:id= "@+id/bt"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "point I add" />    <ScrollViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" >        <LinearLayoutAndroid:id= "@+id/widgets"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" >            <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text="~~~~~~~~~~~" />        </LinearLayout>    </ScrollView></LinearLayout>

Mylinearlayout.java

 Packagecom.example.test;ImportAndroid.content.Context;ImportAndroid.view.LayoutInflater;Importandroid.widget.LinearLayout; Public classMylinearlayoutextendsLinearLayout {PrivateContext Mcontext;  PublicMylinearlayout (Context context) {Super(context); Mcontext=context; Layoutinflater.from (context). Inflate (R.layout.widget, This); }}

Widget.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/tttt"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" >    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "111" />    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "222" /></LinearLayout>

Run results

Android Notes (61) 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.