且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行时向 android xml 视图添加按钮控件?

更新时间:2023-12-06 15:10:04

我看到你在这里做的错误

I see the error u r doing here

LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);

您将布局作为 Linearlayout 对象,您应该将 LinearLayout id 设为

You r taking the layout as Linearlayout object, you should take the LinearLayout id

试试这个

LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);

Button b1 = new Button(this);

b1.setText("Btn");

lnr.addView(b1);