【问题标题】:Printing out a table using TextView and SetText使用 TextView 和 SetText 打印表格
【发布时间】:2013-04-24 00:12:50
【问题描述】:

我正在尝试使用 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);

【问题讨论】:

    标签: android textview settext


    【解决方案1】:

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

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

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

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

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

    您的代码的快速破解方法是:

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

    【讨论】:

      【解决方案2】:

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

      另外,在 LinearLayout 中使用 TableLayout 可能会更好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多