Table 带有文本视图的布局

Table layout with textviews

我有一个包含 3 列的 table 布局,并且我有 3 个字符串数组。我希望每一列都有一个来自字符串数组的每个单元格的文本视图。 到目前为止我的代码:

TableLayout prices = (TableLayout)findViewById(R.id.table);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    table =   getResources().getStringArray(R.array.table);
    for(int i = 0; i < table.length; i++){
        TableRow tr =  new TableRow(this);
        TextView c1 = new TextView(this);
        c1.setText(table[i]);
        TextView c2 = new TextView(this);
        c2.setText (table[i]);
        TextView c3 = new TextView(this);
        c3.setText(table[i]);
        tr.addView(c1);
        tr.addView(c2);
        tr.addView(c3);
        prices.addView(tr);

看起来像这样:

我的字符串数组:

<string-array name="column1">
    <item >5</item>
    <item >7</item>
    <item >10</item>
    <item >18</item>
</string-array>
<string-array name="column2">
    <item >10, 20</item>
    <item >15, 13</item>
    <item >53, 65</item>
    <item >58, 32</item>
</string-array>
<string-array name="column3">
    <item >10, 44</item>
    <item >25, 01</item>
    <item >30, 32</item>
    <item >43, 12</item>
</string-array>

我希望每一列都显示右侧字符串数组中的数字。我该怎么做?

提前致谢。

这取决于您的数据以及您希望如何呈现它。如果您的数据数组只是 1d,而且看起来确实如此,您只需添加第二个 for 循环以查看更多单元格。

for(int i = 0; i < rows; i++){
    TableRow tr =  new TableRow(this);
    for (int j=0; j<columns; j++ {
        TextView c1 = new TextView(this);
        c1.setText(table[i+j]);
        tr.addView(c1);
    }
    prices.addView(tr);
}

或类似的东西。这会一次填充 "table" 一行的单元格。所以这真的取决于你的数据 organization/format。此外,您需要设置列和行的大小,当然还要检查空值,以防您的数据集不是完整的行 * 列条目数。

您的代码将相同的 i 值添加到同一行中的所有 3 个文本视图。这更多的是编写 for 循环的变体而不是当前循环。试试这个。

  table =   getResources().getStringArray(R.array.table);
        String[][] tableData=new String [][]{getResources().getStringArray(R.array.column1),
getResources().getStringArray(R.array.column2),getResources().getStringArray(R.array.column3)
    }
        for(int i = 0; i < tableData[0].length; i++){
             TableRow tr = new TableRow(this);
        for (int j=0;j<tableData.length;j++){              
             TextView c1 = new TextView(this);      
             c1.setText(tableData[j][i]); 
             tr.addView(c1);
            }
            prices.addView(tr);
        } 

因此应该在每一列中为您提供不同的值。只需改变 for 循环即可实现您想要的。您正在按行填充数据。在遍历数组时请记住这一点

好吧,我终于成功了:

TableLayout prices = (TableLayout)findViewById(R.id.table);
    String[] column1 = getResources().getStringArray(R.array.column1);
    String[] column2 = getResources().getStringArray(R.array.column2);
    String[] column3 = getResources().getStringArray(R.array.column3);
    prices.setStretchAllColumns(true);
    prices.bringToFront();
    for(int i = 0; i < column1.length; i++){
        TableRow tr =  new TableRow(this);
        TextView c1 = new TextView(this);
        c1.setText(column1[i]);
        TextView c2 = new TextView(this);
        c2.setText(column2[i]);
        TextView c3 = new TextView(this);
        c3.setText(column3[i]);
        tr.addView(c1);
        tr.addView(c2);
        tr.addView(c3);
        prices.addView(tr);
    }

感谢大家的帮助。