相关疑难解决方法(0)

以编程方式创建TableLayout

我正在尝试以编程方式创建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);
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我没有崩溃,但没有出现.如果我摆脱TableRow实例,至少该按钮确实显示为TableLayout的直接子项.我究竟做错了什么?

android tablelayout

25
推荐指数
2
解决办法
5万
查看次数

标签 统计

android ×1

tablelayout ×1