使用 for 循环使用 TextView 创建线性布局
我想知道是否有一种方法可以在预定义的衬垫布局中使用文本视图动态创建附加的线性布局。这是我的代码,因此您可以了解我要问的要点:
LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId);
for(int i=0; i<5; i++)
{
LinearLayout childLL= new LinearLayout(this);
childLL.setOrientation(LinearLayout.VERTICAL);
childLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
childLL.setGravity(Gravity.LEFT);
TextView text = new TextView(this);
text.setText("The Value of i is :"i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
childLL.addView(text);
MainLL.addView(childLL);
}
我的问题是我只得到“The Value of i is :0”作为输出,即第一个实例。
任何帮助将不胜感激
I was wondering if there is a way to dynamically create an additional linear layout with a textview within a predefined liner layout. THis is my code so you get the gist of what I am asking:
LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId);
for(int i=0; i<5; i++)
{
LinearLayout childLL= new LinearLayout(this);
childLL.setOrientation(LinearLayout.VERTICAL);
childLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
childLL.setGravity(Gravity.LEFT);
TextView text = new TextView(this);
text.setText("The Value of i is :"i);
text.setTextSize(12);
text.setGravity(Gravity.LEFT);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
childLL.addView(text);
MainLL.addView(childLL);
}
My problem is that I am only getting "The Value of i is :0" as the output, i.e. the first instance.
Any help would be much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要将 TextView 包装在另一个 LinearLayout 中,您只需执行以下操作:
You don't need to wrap the TextView inside another LinearLayout, you can do just:
你所做的一切都是正确的,只是
因为你的父布局被第一个视图填充,因为你看不到其他视图。
是的
Everything you are doing is correct just make it
because your parent layout is filled by 1st view because of that you can not see other view.
and yes
是的,如果您非常需要在包装 TextView 之前包装另一个 LinearLayout。
请尝试以下代码:
这将确保包装的 LinearLayout 具有相同的权重,因此所有视图都将显示在屏幕上。
Yes, if you greatly need to wrap another LinearLayout before wrapping the TextView.
Please try this code:
This will ensure the wrapped LinearLayout has the same weight, so all the views will be displayed on the screen.