出了点问题,首先我为表行单独创建了 xml 文件,但它不起作用,然后我以编程方式创建了表行和 TextView ,再次运行时它没有显示,我无法得到如果我的代码有什么问题
table = (TableLayout) findViewById(R.id.goodsTable);
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(getApplicationContext());
atName.setText(attrName[i]);
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
table.requestLayout();
最佳答案
试试这个,也许对你有帮助
for (int i = 0; i < attrName.length; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView atName = new TextView(this);
atName.setText(attrName[i]);
atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(atName);
table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
您必须为 TextView
应用 LayoutParam
并为 TableRow
或 TextView
创建新对象,您应该使用 Activity Context
不是 ApplicationContext
关于android - 动态添加的表在android中不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37786996/