Simple chestnut to understand this automatically generated dynamic control (automatically generated table)
/cs-layout/res/layout/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" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "vertical" > <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:gravity= "Center_vertical"Android:text= "Please enter line:"android:textsize= "20SP" /> <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:hint= "Please enter a number!" "Android:numeric= "decimal" /> </LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Match_parent"Android:layout_marginleft= "10DP"android:gravity= "Center_vertical"Android:text= "Please enter column:"android:textsize= "20SP" /> <EditTextAndroid:id= "@+id/edittext2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:hint= "Please enter a number!" "Android:numeric= "decimal" > <Requestfocus/> </EditText> </LinearLayout> <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "auto-Generate table with one click" /> </LinearLayout> </LinearLayout> <TablelayoutAndroid:id= "@+id/flout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > </Tablelayout></LinearLayout>
Activity_main.xml
/cs-layout/src/com/example/cs_layout/mainactivity.java
Packagecom.example.cs_layout;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;Importandroid.widget.TableLayout;ImportAndroid.widget.TableRow; Public classMainactivityextendsActivity {PrivateButton submit; PrivateEditText Cloumn; PrivateEditText Run; PrivateTablelayout flout; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //cloumn =;Submit =(Button) Findviewbyid (R.id.button1); Cloumn=(EditText) Findviewbyid (R.ID.EDITTEXT1); Run=(EditText) Findviewbyid (R.ID.EDITTEXT2); Flout=(tablelayout) Findviewbyid (r.id.flout); Submit.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {flout.removeallviews (); NewMainactivity (). Shows (Cloumn, run, flout, mainactivity. This); } }); } Public voidshows (EditText Cloumn, EditText Run, tablelayout flout, Activity a) {intc = Integer.parseint (Cloumn.gettext () + "");//converts the incoming number to an integer intR = Integer.parseint (Run.gettext () + ""); Tablelayout Table=NewTablelayout (a);//Table LayoutTable.setweightsum (1);//Weights//table.setcolumnstretchable (0, false); for(inti = 0; I < C; i++) {TableRow tr=NewTableRow (a); for(intj = 0; J < R; J + +) {Button b=NewButton (a); //B.settext (j);Tr.addview (b); } table.addview (TR); } flout.addview (table); } Public voidcolor () {}}
Mainactivity.java
ANDROID Auto generate dynamic table for