如何使用android SDK动态地向TableLayout添加行?

时间:2011-05-08 21:10:36

标签: android dynamic tablelayout

这是我的XML布局代码。我希望能够动态添加TableLayout - tableLayout1的行。请帮忙

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout android:id="@+id/tableLayout1" 
        android:layout_height="match_parent" 
        android:layout_width="wrap_content">
    </TableLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:3)

如果tableLayout1是你的TableLayout,那么

for(int i=0;i<10;i++)
    {
        TableRow tR = new TableRow(this);
        tR.setPadding(5,5,5,5);

        TextView tV_txt1 = new TextView(this);
        TextView tV_txt2 = new TextView(this);


        tV_txt1.setText("This is the textbox1");

        tV_txt2.setText("This is the textbox2");

        tR.addView(tV_txt1);
        tR.addView(tV_txt2);

        tablelayout1.addView(tR);
    }