如何添加一个按钮控件在运行时的机器人XML看法? [英] How to add a button control to an android xml view at runtime?

查看:126
本文介绍了如何添加一个按钮控件在运行时的机器人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);

这篇关于如何添加一个按钮控件在运行时的机器人XML看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆