我如何动态地将行添加到Android SDK的TableLayout?

问题描述:

这是我的XML布局代码。我希望能够动态添加TableLayout - tableLayout1行。请帮助我如何动态地将行添加到Android SDK的TableLayout?

<?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> 

如果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); 
    }