表格布局与textviews

时间:2015-02-23 20:43:05

标签: android textview android-tablelayout

我有一个包含3列的表格布局,我有3个字符串数组。我希望每个列都有一个来自字符串数组的每个单元格的textview。 到目前为止我的代码:

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);

看起来像这样:

enter image description here

我的字符串数组:

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

我希望每列显示正确的字符串数组中的数字。我怎么能这样做?

提前致谢。

3 个答案:

答案 0 :(得分:0)

这取决于您的数据以及您希望如何呈现它。如果您的数据数组只是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”的单元格。所以它真的取决于您的数据组织/格式。此外,您需要设置列和行大小,当然,如果您的数据集不是整行*列条目数,则检查空值。

答案 1 :(得分:0)

您的代码会为同一行中的所有3个文字视图添加相同的i值。这更像是编写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循环即可实现您想要的效果。你正在填写数据行。在迭代数组时记住这一点

答案 2 :(得分:0)

好吧,最后我成功了:

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

感谢您的帮助。