TableLayout (2)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.