我有一个 android xml 布局,main.xml。我想在运行时向此布局添加控件(我想添加一系列包含按钮控件的额外线性布局)。我可以这样做吗?如果可以,怎么做?
谢谢
最佳答案
我看到你在这里做的错误
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
您将布局作为 Linearlayout 对象,您应该采用 LinearLayout id
试试这个
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
关于android - 如何在运行时将按钮控件添加到 android xml View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4924604/