プログラムで 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 の直接の子として表示されます。私は何を間違っていますか?