1

嗨,我想在活动第一次开始时获得一个表格布局来填充 XML 的数据,但我是个新手,所以如果有人知道我怎么可能做到这一点,我真的不知道该怎么做

4

1 回答 1

3

您可以动态添加表格行

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 > 
于 2012-06-03T16:48:03.740 回答