【问题标题】:Bizzare android table row issue奇怪的android表格行问题
【发布时间】:2014-06-08 14:14:28
【问题描述】:

我正在以编程方式尝试将视图添加到我在 Android 中的表格布局中。但我无法向我的行添加超过 1 个视图。它只渲染第一个视图。第二列是空的。我的代码如下。

    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    TableRow tr1 = new TableRow(this);
    TableRow tr2 = new TableRow(this);
    TextView productNumber = new TextView(this);
    productNumber.setText("PRODUCT NUMBER: " + b[0].getProduct_id());
    productNumber.setTextSize(18);
    productNumber.setTextColor(Color.RED);
    productNumber.setPadding(20, 5, 20, 5);
    tr1.addView(productNumber);
    TextView productDesc = new TextView(this);
    productDesc.setText("PRODUCT DESCRIPTION: " + b[0].getItem_description());
    productDesc.setTextSize(18);
    productDesc.setTextColor(Color.RED);
    productDesc.setPadding(20, 5, 20, 5);
    tr2.addView(productDesc);
    productNumber.setLayoutParams(lp);
    productDesc.setLayoutParams(lp);
    tl.setStretchAllColumns(true);
    tl.addView(tr1, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tl.addView(tr2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));



    for (ProductBean p: b) {
        TableRow lineSeparator = new TableRow(this.getApplicationContext());
        View line = new View(this.getApplicationContext());
        line.setMinimumHeight(1);
        line.setBackgroundColor(Color.WHITE);
        line.setPadding(0, 5, 0, 5);
        lineSeparator.addView(line);
        tl.addView(lineSeparator);
        Set<String> headers = p.getAttr().keySet();

    //Till here it works fine. The following code seems to not add more than 1 view

        for (String s: headers) {
            Log.i("S", "S: " + p.getAttr().get(s));
            TableRow tableRow = new TableRow(getApplicationContext());
            TextView textView = new TextView(getApplicationContext());
            textView.setText(s);
            textView.setPadding(20, 5, 20, 5);
            textView.setTextSize(15);
            tableRow.addView(textView);
            textView = new TextView(getApplicationContext());
            textView.setText(p.getAttr().get(s));
            textView.setPadding(20, 5, 20, 5);
            textView.setTextSize(15);
            tableRow.addView(textView); //This view is not rendered.
            tl.addView(tableRow);
        }
    }

但是,我在单独的活动和布局中尝试了以下操作,并且成功运行。 Hello 是一列,World 是另一列。

    for (int i=0; i<5; i++) {
        TableRow tableRow = new TableRow(getApplicationContext());
        TextView textView = new TextView(getApplicationContext());
        textView.setText("Hello");
        tableRow.addView(textView);
        textView = new TextView(getApplicationContext());
        textView.setText("World"); 
        tableRow.addView(textView);
        tableLayout.addView(tableRow);
    }

为什么我的代码没有渲染第二个视图呢?我能够记录两个文本视图的内容,但它只是不呈现行中的第二个视图。请帮忙。我完全迷路了,已经为此苦恼了 12 个小时!!

【问题讨论】:

  • 为什么要在 for 循环中添加两次 tableRow.addView(textView);
  • 这就是添加第二列的方式。您可以尝试运行第二个代码 sn -p。工作得很好。基本上,一旦 textView 完成,添加它,然后重新定义它以添加另一列。
  • @AS 并且您设置相同的填充 textView.setPadding(20, 5, 20, 5); 将填充更改为第二个 TextView 并尝试。
  • p.getAttr().get(s) 返回的对象的类型是什么?您是否尝试添加 toString()? p.getAttr().get(s).toString() 似乎文本可能被设置为空,因此您在表格行中看不到它。
  • 如我所料,更改填充不起作用。为什么会呢?如果您查看第二个 sn-p,则没有填充。所以这两个视图都有相同的填充,这是没有的。如果这有效,为什么填充会有任何不同。关键是由于某种原因,我的代码中没有呈现 textView 的第二个实例。

标签: android android-view android-tablelayout tablerow


【解决方案1】:

我也想到了问题和解决方案。实际上,我的 xml 中只有一个滚动视图和一个表格布局。我添加的前两个文本视图,然后是我添加的表格行,将跨越整个屏幕的宽度,因此表格的第一列将占据整个屏幕的宽度。

   tl.addView(tr1, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
   tl.addView(tr2, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

现在我在滚动视图之外创建了一个单独的LinearLayout,并将上面这两行的内容添加到LinearLayout中。工作正常!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多