android动态生成表格,使用的是TABLELAYOUT

使用tablelayout及tablerow生成表格,这里我只生成了一次,可以根据需求更改哦....,对于里面的控件是可以监听的....
mainactivity代码如下:
[html] 
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布局如下:
[html] 
<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> 
 
效果图如下: 

 

转自:http://www.2cto.com/kf/201208/149308.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值