Android TableLayout 不显示

发布于 2024-12-14 04:39:05 字数 1624 浏览 1 评论 0原文

我有一个类,其内容设置为一个布局,其中有几个按钮和一个 TableLayout。

制作 TableLayout 的真正工作是在一个单独的静态帮助器类中,该类具有返回所需表格的方法。

但是,该表未显示。我错过了什么简单得丢脸的事实?

这是其内容设置为布局的类:

public class TesterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TableLayout table = (TableLayout) findViewById(R.id.table);
        table = TableHelper.getTable_BINARY_NUMBERS(getApplicationContext(), 5, 25);
    }
}

这是创建 Table 的主要内容的辅助类:

public class TableHelper {

    public static TableLayout getTable_BINARY_NUMBERS(Context context, int numRows, int numCols) {

        TableLayout table = new TableLayout(context);

        table.setStretchAllColumns(true);  
        table.setShrinkAllColumns(true);  

        TableRow[] rows = new TableRow[numRows];
        for (int row=0; row<numRows; row++) {
            rows[row] = new TableRow(context);

            for (int col=0; col<numCols-1; col++) {
                TextView num = new TextView(context);
                num.setText("0");
                rows[row].addView(num);
            }
            TextView rowText = new TextView(context);
            rowText.setText("Row " + (row + 1));
            rowText.setTextAppearance(context, android.R.style.TextAppearance_Small);
            rows[row].addView(rowText);
            rows[row].setPadding(0, 50, 0, 0);
            table.addView(rows[row]);
        }
        return table;
    }
}

I've got a class whose content is set to a layout which has a few buttons and a TableLayout.

The real work that makes the TableLayout is in a separate static helper class, which has a method that returns the desired table.

However, the table is not displaying. What humiliatingly simple fact am I missing?

Here is the class whose content is set to the layout:

public class TesterActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TableLayout table = (TableLayout) findViewById(R.id.table);
        table = TableHelper.getTable_BINARY_NUMBERS(getApplicationContext(), 5, 25);
    }
}

And here is the helper class that creates the Table's meat:

public class TableHelper {

    public static TableLayout getTable_BINARY_NUMBERS(Context context, int numRows, int numCols) {

        TableLayout table = new TableLayout(context);

        table.setStretchAllColumns(true);  
        table.setShrinkAllColumns(true);  

        TableRow[] rows = new TableRow[numRows];
        for (int row=0; row<numRows; row++) {
            rows[row] = new TableRow(context);

            for (int col=0; col<numCols-1; col++) {
                TextView num = new TextView(context);
                num.setText("0");
                rows[row].addView(num);
            }
            TextView rowText = new TextView(context);
            rowText.setText("Row " + (row + 1));
            rowText.setTextAppearance(context, android.R.style.TextAppearance_Small);
            rows[row].addView(rowText);
            rows[row].setPadding(0, 50, 0, 0);
            table.addView(rows[row]);
        }
        return table;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

听你说爱我 2024-12-21 04:39:05

不要返回新的表格布局,而是将立即获得的表格布局传递给 getTable_BINARY_NUMBERS() 并在方法中修改它,而不是返回全新的表格布局。

Instead of returning a new table layout, pass the one you get right away to your getTable_BINARY_NUMBERS() and modify it in your method instead of returning a completely new one.

樱&纷飞 2024-12-21 04:39:05

您的帮助程序类返回的 TableLayout 与您看到的 TableLayout 不同。

当您执行 setContentView(R.layout.main); 时,会创建一个 TableLayout 实例,并将其分配给变量表。来自辅助类的 TableLayout 是一个不同的实例。

The TableLayout your helper class is returning is not the same as the TableLayout that you are seeing.

when you do setContentView(R.layout.main); an instance of TableLayout is created which you assign to the variable table. The TableLayout coming from your helper class is a different instance.

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