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> 
 
效果圖如下: 

 
 

發佈留言