【发布时间】:2010-12-04 11:30:47
【问题描述】:
我正在尝试以编程方式创建 TableLayout。它只是行不通。 xml 文件中的相同布局虽然有效。这就是我所拥有的:
public class MyTable extends TableLayout
{
public MyTable(Context context) {
super(context);
setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TableRow row = new TableRow(context);
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button b = new Button(getContext());
b.setText("hello");
b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(b);
addView(row)
}
}
...
// In main activity:
MyTable table = new MyTable(this);
mainLayout.addView(table);
当我运行它时,我没有崩溃,但什么也没有出现。如果我摆脱 TableRow 实例,至少该按钮确实显示为 TableLayout 的直接子级。我做错了什么?
【问题讨论】:
标签: android tablelayout