Tablelayout and tablerow are used to generate a table. Here I only generate a table once. You can change it as needed. The controls in the table can be monitored ....
The mainactivity code is as follows:
[Html]
Package com. xy. tablerow;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. graphics. Color;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. ImageView;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. support. v4.app. NavUtils;
Public class MainActivity extends Activity implements OnClickListener {
Button bu;
Boolean mFlag = false;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button mButton = (Button) findViewById (R. id. myButton );
MButton. setOnClickListener (this );
Bu = new Button (MainActivity. this );
Bu. setText ("tianjia ");
Bu. setId (0x10 );
Bu. setOnClickListener (this );
}
Public void addWegit (){
TableLayout table = (TableLayout) findViewById (R. id. tablelayout );
Table. setStretchAllColumns (true );
For (int I = 0; I <6; I ++ ){
TableRow tablerow = new TableRow (MainActivity. this );
Tablerow. setBackgroundColor (Color. rgb (222,220,210 ));
For (int j = 0; j <3; j ++ ){
If (I = 0 & j = 0 ){
Tablerow. addView (bu );
} Else {
EditText testview = new EditText (MainActivity. this );
Testview. setBackgroundResource (R. drawable. shape );
// Testview. setText ("select ");
Tablerow. addView (testview );
}
}
Table. addView (tablerow, new TableLayout. LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT ));
}
}
@ Override
Public void onClick (View v ){
Int vv = v. getId ();
Switch (vv ){
Case 0x10:
Toast. makeText (MainActivity. this, "heh, keyidianjide ",
Toast. LENGTH_SHORT). show ();
Break;
Case R. id. myButton:
If (! MFlag ){
AddWegit ();
MFlag =! MFlag;
}
Break;
}
}
}
The xml layout is as follows:
[Html]
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/myButton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "dynamic Table Generation"/>
<TableLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "# dedcd2"
Android: stretchColumns = "*">
<TableRow
Android: layout_margin = "0.5dip"
Android: background = "# dedcd2">
<TextView
Android: background = "# ffffff"
Android: gravity = "center"
Android: text = "year"
Android: textSize = "20dip"
Android: textStyle = "bold"/>
<TextView
Android: gravity = "center"
Android: background = "# ffffff"
Android: text = "principal"
Android: textSize = "20dip"
Android: textStyle = "bold"/>
<TextView
Android: gravity = "center"
Android: background = "# ffffff"
Android: text = "interest"
Android: textSize = "20dip"
Android: textStyle = "bold"/>
</TableRow>
</TableLayout>
<TableLayout
Android: id = "@ + id/tablelayout"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: background = "# dedcd2"
Android: stretchColumns = "*">
<TableRow
Android: layout_margin = "0.5dip"
Android: background = "# dedcd2">
<TextView
Android: layout_margin = "1dip"
Android: background = "# ffffff"
Android: text = ""
Android: textSize = "20dip"
Android: textStyle = "bold"/>
<TextView
Android: layout_margin = "1dip"
Android: background = "# ffffff"
Android: text = ""
Android: textSize = "20dip"
Android: textStyle = "bold"/>
<TextView
Android: layout_margin = "1dip"
Android: background = "# ffffff"
Android: text = ""
Android: textSize = "20dip"
Android: textStyle = "bold"/>
</TableRow>
</TableLayout>
</LinearLayout>
As follows: