TableLayout 创建表格

公司用到创建表格,我就从项目中抽出来,做了一个实例。

废话不多说,先上效果图:

TableLayout 创建表格_第1张图片


关键的代码如下:

public class MainActivity extends Activity {

	private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
	private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
	TableLayout tableLayout;
	TextView showText;
	TableRow tableRow;
	String tr;// 获取的数据

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tableLayout = (TableLayout) findViewById(R.id.TableLayout01);
		showText = (TextView) findViewById(R.id.text1);
		initView();

	}

	private void initView() {
		tableLayout.setStretchAllColumns(true);
		for (int row = 0; row < 4; row++) {
			tableRow = new TableRow(MainActivity.this);
			tableRow.setBackgroundColor(Color.rgb(222, 220, 210));
			for (int col = 0; col < 3; col++) {
				TextView tv = new TextView(MainActivity.this);
				tv.setBackgroundResource(R.drawable.shapee);
				tv.setText("" + col + "" + row);
				tv.setGravity(Gravity.CENTER);
				tv.setTextColor(Color.RED);
				tableRow.addView(tv);
			}
			tableLayout.addView(tableRow, new LayoutParams(FP, WC));
		}
		
	}

}


如果你想看例子 点击下载






你可能感兴趣的:(移动开发)