使用tablelayout及tablerow生成表格,这里我只生成了一次,可以根据需求更改哦....,对于里面的控件是可以监听的....
mainactivity代码如下:
- package com.xy.tablerow;
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Color;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.support.v4.app.NavUtils;
- public class MainActivity extends Activity implements OnClickListener {
- Button bu;
- boolean mFlag = false;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button mButton = (Button) findViewById(R.id.myButton);
- mButton.setOnClickListener(this);
- bu = new Button(MainActivity.this);
- bu.setText("tianjia");
- bu.setId(0x10);
- bu.setOnClickListener(this);
- }
- public void addWegit() {
- TableLayout table = (TableLayout) findViewById(R.id.tablelayout);
- table.setStretchAllColumns(true);
- for (int i = 0; i < 6; i++) {
- TableRow tablerow = new TableRow(MainActivity.this);
- tablerow.setBackgroundColor(Color.rgb(222, 220, 210));
- for (int j = 0; j < 3; j++) {
- if (i == 0 && j == 0) {
- tablerow.addView(bu);
- } else {
- EditText testview = new EditText(MainActivity.this);
- testview.setBackgroundResource(R.drawable.shape);
- // testview.setText("选择");
- tablerow.addView(testview);
- }
- }
- table.addView(tablerow, new TableLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- }
- }
- @Override
- public void onClick(View v) {
- int vv = v.getId();
- switch (vv) {
- case 0x10:
- Toast.makeText(MainActivity.this, "heh,keyidianjide",
- Toast.LENGTH_SHORT).show();
- break;
- case R.id.myButton:
- if (!mFlag) {
- addWegit();
- mFlag = !mFlag;
- }
- break;
- }
- }
- }
xml布局如下:
- <LinearLayout 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"
- android:orientation="vertical" >
- <Button
- android:id="@+id/myButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="动态生成表格" />
- <TableLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#dedcd2"
- android:stretchColumns="*" >
- <TableRow
- android:layout_margin="0.5dip"
- android:background="#dedcd2" >
- <TextView
- android:background="#ffffff"
- android:gravity="center"
- android:text="年度"
- android:textSize="20dip"
- android:textStyle="bold" />
- <TextView
- android:gravity="center"
- android:background="#ffffff"
- android:text="本金"
- android:textSize="20dip"
- android:textStyle="bold" />
- <TextView
- android:gravity="center"
- android:background="#ffffff"
- android:text="利息"
- android:textSize="20dip"
- android:textStyle="bold" />
- </TableRow>
- </TableLayout>
- <TableLayout
- android:id="@+id/tablelayout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#dedcd2"
- android:stretchColumns="*" >
- <TableRow
- android:layout_margin="0.5dip"
- android:background="#dedcd2" >
- <TextView
- android:layout_margin="1dip"
- android:background="#ffffff"
- android:text=""
- android:textSize="20dip"
- android:textStyle="bold" />
- <TextView
- android:layout_margin="1dip"
- android:background="#ffffff"
- android:text=""
- android:textSize="20dip"
- android:textStyle="bold" />
- <TextView
- android:layout_margin="1dip"
- android:background="#ffffff"
- android:text=""
- android:textSize="20dip"
- android:textStyle="bold" />
- </TableRow>
- </TableLayout>
- </LinearLayout>
- 效果图如下: