【问题标题】:Adding TableRows dynamically in Android doesn't work在 Android 中动态添加 TableRows 不起作用
【发布时间】:2013-12-10 23:55:56
【问题描述】:

我在 android 中将 tablerows 添加到表格布局时遇到了一点问题。

这是我的代码:

int z = 0;
        for(String detail: details){
            TableRow tr = new TableRow(this);
            tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            //odd / even
            if(z%2 == 0)
                tr.setBackgroundColor(Color.parseColor("#F5F5F5"));

            //set detail text
            TextView detailText = new TextView(this);
            RelativeLayout.LayoutParams dtlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        
            detailText.setTextSize(16.0f);
            detailText.setTextColor(Color.parseColor("#333333"));
            detailText.setPadding(20, 10, 20, 10);
            detailText.setLayoutParams(dtlp);
            detailText.setText(detail);
            tr.addView(detailText);

            detailsTable.addView(tr);

            ++z;
        }

我不知道问题出在哪里。正在设置详细文本视图,但表格行不会显示。

【问题讨论】:

  • 上面的代码一切正常,你必须使用 LayoutParams,也试试这个 detailsTable.addView(tr, new TableLayout.LayoutParams(...,....) )
  • 检查 YOUR_LAYOUT.xml 中的 TableLayout 高度和宽度,因为您已将 TableRow 宽度设置为 MATCH_PARENT 更改它到 WRAP_CONTENT。

标签: android android-tablelayout


【解决方案1】:

一个。尝试将 dtlp 的“width parameter”更改为“RelativeLayout.MATCH_PARENT

b.在 For 循环块之前创建两个这样的布局参数。

TableLayout.LayoutParams tlps=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);

TableRow.LayoutParams trps=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);

循环替换

  detailText.setLayoutParams(dtlp);

  detailText.setLayoutParams(trps);

并替换

   detailsTable.addView(tr);

    detailsTable.addView(tr,tlps);

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 2018-06-01
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多