由代码生成的 TableLayout 未显示
我需要使用代码动态生成表格布局。我无法让它显示。我已将表生成代码段减少到其基础,以尝试将其解决,但即使这个大大简化的版本也无法工作(即不显示)。有人可以指出我做错了什么吗?谢谢。
public class TableByCodeTest extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow firstTableRow = new TableRow(this);
firstTableRow.setLayoutParams(
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView title = new TextView(this);
title.setText(R.string.title);
title.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
firstTableRow.addView(title);
tableLayout.addView(firstTableRow);
setContentView(tableLayout);
}
}
I have a need to generate a table layout dynamically using code. I can't get it to display. I've reduced the table generation code segment down to its basics to try and nut it out but even this greatly simplified version isn't working (ie. doesn't display). Can someone please point out what I'm doing wrong? Thanks.
public class TableByCodeTest extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TableRow firstTableRow = new TableRow(this);
firstTableRow.setLayoutParams(
new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
TextView title = new TextView(this);
title.setText(R.string.title);
title.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
firstTableRow.addView(title);
tableLayout.addView(firstTableRow);
setContentView(tableLayout);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将行:更改
为
将其添加到第 0 行。
Change the line:
to
To add it to row 0.