0

我正在尝试使用 TextView 和 setText 打印表格,但无法正确打印表格。它只打印出一行。据我了解, setText 会覆盖上一行吗?我怎样才能解决这个问题,以便我的 for 循环打印出每一行而不是仅仅覆盖当前行?

    ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    TextView tv = new TextView(this);
    tv.setText("Character \t Correct \t Incorrect \t Percentage\n");
    ll.addView(tv);

    for(int counter = 0; counter<scores1.length;counter++){
        tv.setText(level1[counter] + "\t " + correct1[counter] + "\t " + incorrect1[counter] + "\t " + percentage1[counter] + "\n");
    }

    this.setContentView(sv);
4

2 回答 2

1

对于每一列,您需要setText()在该实例下实例化一个新的 TextView。

此外,也许最好在 LinearLayout中使用TableLayout 。

于 2013-04-24T00:19:23.640 回答
1

您应该考虑使用 a ListView,请参阅

http://developer.android.com/guide/topics/ui/layout/listview.html

Android页面上有一个示例ListView,您也可以查看:

http://www.vogella.com/articles/AndroidListView/article.html#listview_listviewexample

ListView负责TextView为您创建 s 并重新使用它们以节省内存并提供适配器以使用来自数据库和其他来源的数据。

您的代码的快速破解将是:

for(int counter = 0; counter<scores1.length;counter++){
    tv = new TextView(this);
    ll.addView(tv);
    tv.setText(level1[counter] + "\t " + correct1[counter] + "\t " + incorrect1[counter] + "\t " + percentage1[counter] + "\n");
}
于 2013-04-24T00:29:23.237 回答