Android上如何动态添加LinearLayout?
我有一个长度为 n 的数组,现在需要创建 n 个 LinearLayout 并在每个布局上添加不同的内容。 怎样才能动态地完成呢?
I have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them.
How can it be done dynamically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是在 xml 中创建布局并使用
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
对其进行扩充,您可能还需要
setId()
code> 您添加的视图,以便您稍后可以轻松访问它们。The easiest way is to create a layout in xml and inflate it using
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
You may also want to
setId()
your added views so you can access them easily later on.我使用RelativeLayout解决了这个问题,我发现它更容易使用。是的,当然就像上面指出的那样,我使用了
setId()
。这是我实现的代码:I solved it using RelativeLayout which I found a little easier to work with. Yes of-course like the guys pointed out above I used
setId()
. Here is the code I implemented: