微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 动态添加的表行没有出现

我看过很多关于动态添加表行的帖子,但我不确定我缺少什么.

当我执行以下操作时,不显示任何内容(除了应用程序标题栏).

我的布局:

我的活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.table_view);

    TableLayout tableLayout = (TableLayout) findViewById(R.id.tvt_tableview);

    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));

    TextView column1 = new TextView(this);
    column1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    column1.setBackgroundColor(Color.YELLOW);
    column1.setTextColor(Color.BLUE);
    column1.setText("Col1 Value");
    tableRow.addView(column1);

    TextView column2 = new TextView(this);
    column2.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    column2.setBackgroundColor(Color.RED);
    column2.setTextColor(Color.GREEN);
    column2.setText("Col2 Value");
    tableRow.addView(column2);

    tableLayout.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

    //tl.refreshDrawableState();
    //findViewById(R.id.tvt_scroll_relative).refreshDrawableState();
}
最佳答案
更改两个引用

ViewGroup.LayoutParams

TableRow.LayoutParams

它应该工作.

将布局参数添加到窗口小部件需要LayoutParams是周围布局容器的内部类.

原文地址:https://www.jb51.cc/android/430894.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐