如何添加一个按钮控件在运行时的机器人XML看法?

问题描述:

我有一个Android XML布局,main.xml中。我想控件添加到这个布局在运行时(我想补充的一系列包含按钮控件的其他线性布局)。我能做到这一点,如果是的话,怎么办?

I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?

感谢

我看到错误UR在这里做什么

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);