Layout.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "6" > <tablelayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:layout_gravity= "Center_vertical" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please enter the row number column to generate the table"android:textsize= "15sp"/> <TableRow android:gravity= "Center"Android:layout_margintop= "5DP" > <TextView Android:id= "@+id/tv1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "line"android:textsize= "15sp"/> <EditText Android:id= "@+id/et1"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"/> </TableRow> <TableRow android:gravity= "Center"Android:layout_margintop= "5DP" > <TextView Android:id= "@+id/tv2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "column"android:textsize= "15sp"/> <EditText Android:id= "@+id/et2"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"Android:numeric= "decimal"/> <!--decimal can only enter a number, greater than 0 of the number--</TableRow> <TableRow android:gravity= "Center" > <Button Android:id= "@+id/btnadd"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_span= "2"Android:text= "OK"/> </TableRow> </TableLayout> </linea Rlayout> <tablelayout Android:id= "@+id/table"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_weight= "3"Android:stretchcolumns="*"Android:shrinkcolumns= "*" > </TableLayout></LinearLayout>
Activity.java
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 classMainactivityextendsActivityImplementsOnclicklistener {PrivateButton submit; PrivateEditText Row; PrivateEditText Cloumn; Privatetablelayout table; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.canchange); Submit=(Button) Findviewbyid (R.id.btnadd); Row=(EditText) Findviewbyid (R.ID.ET1); Cloumn=(EditText) Findviewbyid (R.id.et2); Table=(tablelayout) Findviewbyid (r.id.table); Submit.setonclicklistener ( This); } @Override Public voidOnClick (View v) {intc = Integer.parseint (Row.gettext () + ""); intR = Integer.parseint (Cloumn.gettext () + ""); Table.removeallviews (); for(inti = 0; I < C; i++) {TableRow tr=NewTableRow ( This); for(intj = 0; J < R; J + +) {Button b=NewButton ( This); Tr.addview (b); } table.addview (TR); } }}
Effect:
When you don't know Lingyunmu, you should be straight to the beginning of the road high
Android Auto Generate table