.java代码如下:
package org.lxh.demo;
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 MyTableLayoutDemo extends Activity {
private String titleData[][] = new String[][] {
{ "ID", "姓名", "EMAIL", "地址" },
{ "01", "小李", "[email protected]",
"北京" },
{ "02", "小张", "[email protected]", "天津" } }; // 定义要显示的数据
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout(this); // 定义表格布局
TableLayout.LayoutParams layoutParam = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT); // 定义布局管理器的参数
//layout.setBackgroundResource(R.drawable.mldn_logo); // 定义背景图片
for (int x = 0; x < this.titleData.length; x++) { // 循环设置表格行
TableRow row = new TableRow(this); // 定义表格行
for (int y = 0; y < this.titleData[x].length; y++) {
TextView text = new TextView(this);
text.setText(this.titleData[x][y]); // 设置文本内容
row.addView(text, y); // 加入一个编号
}
layout.addView(row); // 向表格之中增加若干个表格行
}
super.setContentView(layout, layoutParam); // 设置显示
}
}
代码下载链接:http://download.csdn.net/detail/yayun0516/8362777
原文:http://blog.csdn.net/yayun0516/article/details/42720141