2011-05-08 47 views
1

這是我的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> 

回答

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); 
    } 
相關問題