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