In the previous article, we mainly designed the UI Designer to design TableLayout + TableRow. in actual application, we often need to add data to TableLayout in the Code. (You can also use TableLayout to create the 9th palace chart ), this article describes how to use this method.
The main. xml Code is as follows. The ID of TableLayout is TableLayout01:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TableLayout
Android: id = "@ + id/TableLayout01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</TableLayout>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TableLayout
Android: id = "@ + id/TableLayout01"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
</TableLayout>
</LinearLayout>
The JAVA code is as follows:
View plaincopy to clipboardprint?
Package com. LayoutDemo;
Import com. LayoutDemo. R;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. ViewGroup;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Public class LayoutDemo extends Activity {
/** Called when the activity is first created .*/
Private final int WC = ViewGroup. LayoutParams. WRAP_CONTENT;
Private final int FP = ViewGroup. LayoutParams. FILL_PARENT;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Create a TableLayout01 instance
TableLayout tableLayout = (TableLayout) findViewById (R. id. TableLayout01 );
// All columns are automatically filled with spaces
TableLayout. setStretchAllColumns (true );
// Generate a table with 10 rows and 8 Columns
For (int row = 0; row <10; row ++)
{
TableRow tableRow = new TableRow (this );
For (int col = 0; col <8; col ++)
{
// TV for display
TextView TV = new TextView (this );
TV. setText ("(" + col + "," + row + ")");
TableRow. addView (TV );
}
// Add the newly created TableRow to TableLayout
TableLayout. addView (tableRow, new TableLayout. LayoutParams (FP, WC ));
}
}
}
Package com. LayoutDemo;
Import com. LayoutDemo. R;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. ViewGroup;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Public class LayoutDemo extends Activity {
/** Called when the activity is first created .*/
Private final int WC = ViewGroup. LayoutParams. WRAP_CONTENT;
Private final int FP = ViewGroup. LayoutParams. FILL_PARENT;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Create a TableLayout01 instance
TableLayout tableLayout = (TableLayout) findViewById (R. id. TableLayout01 );
// All columns are automatically filled with spaces
TableLayout. setStretchAllColumns (true );
// Generate a table with 10 rows and 8 Columns
For (int row = 0; row <10; row ++)
{
TableRow tableRow = new TableRow (this );
For (int col = 0; col <8; col ++)
{
// TV for display
TextView TV = new TextView (this );
TV. setText ("(" + col + "," + row + ")");
TableRow. addView (TV );
}
// Add the newly created TableRow to TableLayout
TableLayout. addView (tableRow, new TableLayout. LayoutParams (FP, WC ));
}
}
}
The result is as follows: