TableLayout is set to the bottom, TableRow is set to the top of TableLayout, and buttons, TextView, and other controls are set to the top of TableLayout,
In addition, you can also place controls on TableLayout separately. TableLayout is a complex layout with the simplest usage only
Only the drag control makes an interface, but in fact, TableLayout is often used in the Code, for example, to make a table.
Android: collapseColumns: hides the specified column in the order of 0th actions
Android: shrinkColumns: automatically extends the specified column to fill the available part in The 0th action order.
Android: stretchColumns: In The 0th action order, try to fill the specified column with a blank part.
Ii. solution 1 code display:
Solution 1 uses xml layout. In addition to layout, no layout operations are performed in the code.
1. "Acticity_06srcyanacticity_06MainActivity.java"
[Java]
Package yan. activity_06;
Import android. OS. Bundle;
Import android. app. Activity;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
}
Package yan. activity_06;
Import android. OS. Bundle;
Import android. app. Activity;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
}
2. "Activity_06reslayoutactivity_main.xml"
[Html]
<TableLayout 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"
Tools: context = ". MainActivity"
Android: stretchColumns = "0">
<TableRow>
<TextView
Android: text = "first row, first column"
Android: background = "# aa0000"
Android: padding = "3dip"/>
<TextView
Android: text = "second column of the First row"
Android: padding = "3dip"
Android: gravity = "center_horizontal"
Android: background = "#00aa00"
> </TextView>
<TextView
Android: text = "the third column in the first row"
Android: gravity = "right"
Android: background = "# tianaa"
Android: padding = "3dip"/>
</TableRow>
<TableRow>
<TextView
Android: text = "first column of the Second row"
Android: padding = "3dip"/>
<TextView
Android: text = "second row and second column"
Android: gravity = "right"
Android: padding = "3dip"/>
</TableRow>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</TableLayout>
<TableLayout 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"
Tools: context = ". MainActivity"
Android: stretchColumns = "0">
<TableRow>
<TextView
Android: text = "first row, first column"
Android: background = "# aa0000"
Android: padding = "3dip"/>
<TextView
Android: text = "second column of the First row"
Android: padding = "3dip"
Android: gravity = "center_horizontal"
Android: background = "#00aa00"
> </TextView>
<TextView
Android: text = "the third column in the first row"
Android: gravity = "right"
Android: background = "# tianaa"
Android: padding = "3dip"/>
</TableRow>
<TableRow>
<TextView
Android: text = "first column of the Second row"
Android: padding = "3dip"/>
<TextView
Android: text = "second row and second column"
Android: gravity = "right"
Android: padding = "3dip"/>
</TableRow>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
</TableLayout>
Iii. solution 2 code display:
Solution 2 uses the code layout. In addition to displaying an empty TabLayout in the layout file, no layout is made.
1. "Acticity_06srcyanacticity_06MainActivity.java"
[Java]
Package yan. activity_06;
Import android. OS. Bundle;
Import android. view. ViewGroup;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Import android. app. Activity;
Public class MainActivity 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. activity_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 yan. activity_06;
Import android. OS. Bundle;
Import android. view. ViewGroup;
Import android. widget. TableLayout;
Import android. widget. TableRow;
Import android. widget. TextView;
Import android. app. Activity;
Public class MainActivity 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. activity_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 ));
}
}
}
2. "Activity_06reslayoutactivity_main.xml"
[Html]
<? 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>
Iii. effect display:
1. solution 1:
2. solution 2: