使用 for 循环使用 TextView 创建线性布局

发布于 2024-10-10 03:40:10 字数 790 浏览 0 评论 0原文

我想知道是否有一种方法可以在预定义的衬垫布局中使用文本视图动态创建附加的线性布局。这是我的代码,因此您可以了解我要问的要点:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我只土不豪 2024-10-17 03:40:10

您不需要将 TextView 包装在另一个 LinearLayout 中,您只需执行以下操作:

LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 
  for(int i=0; i<5; i++){
 TextView text = new TextView(this);
   text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign?
   text.setTextSize(12);  
   text.setGravity(Gravity.LEFT);
   text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
MainLL.addView(text);
}

You don't need to wrap the TextView inside another LinearLayout, you can do just:

LinearLayout MainLL= (LinearLayout) findViewById(R.id.myLayoutId); 
  for(int i=0; i<5; i++){
 TextView text = new TextView(this);
   text.setText("The Value of i is :"+i); // <-- does it really compile without the + sign?
   text.setTextSize(12);  
   text.setGravity(Gravity.LEFT);
   text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
MainLL.addView(text);
}
掌心的温暖 2024-10-17 03:40:10

你所做的一切都是正确的,只是

childLL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

因为你的父布局被第一个视图填充,因为你看不到其他视图。

是的

 text.setText("The Value of i is :"+i); //add + sign

Everything you are doing is correct just make it

childLL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  

because your parent layout is filled by 1st view because of that you can not see other view.

and yes

 text.setText("The Value of i is :"+i); //add + sign
怼怹恏 2024-10-17 03:40:10

是的,如果您非常需要在包装 TextView 之前包装另一个 LinearLayout。
请尝试以下代码:

childLL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));  

这将确保包装的 LinearLayout 具有相同的权重,因此所有视图都将显示在屏幕上。

Yes, if you greatly need to wrap another LinearLayout before wrapping the TextView.
Please try this code:

childLL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));  

This will ensure the wrapped LinearLayout has the same weight, so all the views will be displayed on the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文