如何在运行时向 android xml 视图添加按钮控件? [英] How to add a button control to an android xml view at runtime?
本文介绍了如何在运行时向 android 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?
谢谢
推荐答案
我看到你在这里做的错误
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);
这篇关于如何在运行时向 android xml 视图添加按钮控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文