由代码生成的 TableLayout 未显示

发布于 2024-09-16 12:44:21 字数 941 浏览 1 评论 0原文

我需要使用代码动态生成表格布局。我无法让它显示。我已将表生成代码段减少到其基础,以尝试将其解决,但即使这个大大简化的版本也无法工作(即不显示)。有人可以指出我做错了什么吗?谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世俗缘 2024-09-23 12:44:21

将行:更改

firstTableRow.addView(title);

firstTableRow.addView(title,new TableRow.LayoutParams(0));

将其添加到第 0 行。

Change the line:

firstTableRow.addView(title);

to

firstTableRow.addView(title,new TableRow.LayoutParams(0));

To add it to row 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文