Android TableLayout 不显示
我有一个类,其内容设置为一个布局,其中有几个按钮和一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要返回新的表格布局,而是将立即获得的表格布局传递给
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.您的帮助程序类返回的
TableLayout
与您看到的TableLayout
不同。当您执行
setContentView(R.layout.main);
时,会创建一个TableLayout
实例,并将其分配给变量表。来自辅助类的TableLayout
是一个不同的实例。The
TableLayout
your helper class is returning is not the same as theTableLayout
that you are seeing.when you do
setContentView(R.layout.main);
an instance ofTableLayout
is created which you assign to the variable table. TheTableLayout
coming from your helper class is a different instance.