以编程方式将行添加到表布局

时间:2012-06-03 16:44:46

标签: android tablelayout

您好我希望得到一个表格布局,以便在活动首次启动时填充XML的数据,但我是一个新手,所以我真的不知道如果有人知道我怎么可能做到这一点此

1 个答案:

答案 0 :(得分:2)

您可以动态添加表格行

for(int i=0;i<youxmlArray.size();i++){
    // get a reference for the TableLayout
    TableLayout table = (TableLayout)findViewById(R.id.TableLayout01);
    // create a new TableRow
    TableRow row = new TableRow(this);
    // create a new TextView for showing xml data
    TextView t = new TextView(this);
    // set the text to "text xx"
    t.setText( "YOUR XML DATA  HERE");
    // add the TextView  to the new TableRow
    row.addView(t);
    // add the TableRow to the TableLayout
    table.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
}

并在xml布局中添加 TableLayout

<TableLayout 
android:id = "@+id/TableLayout01" 
android:layout_width = "fill_parent" 
android:layout_height = "wrap_content"   
android:stretchColumns = "0" >
   < TableRow android:id = "@+id/TableRow01" 
   android:layout_width = "wrap_content" 
   android:layout_height = "wrap_content" >
      < TextView android:id = "@+id/TextView01" 
        android:layout_width = "fill_parent" 
        android:layout_height = "wrap_content" 
        android:text = "textfield 1-1" >
        </ TextView >
   </ TableRow >
</ TableLayout >