用一个for循环TextViews创建线性布局 [英] Creating Linear Layout with TextViews using a for loop

查看:283
本文介绍了用一个for循环TextViews创建线性布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有一种方法可以动态地创建一个predefined衬垫布局中一个TextView额外的线性布局。这是我的code等你拿我是问什么的要点是:

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);
}

我的问题是,我只得到我的值是:0作为输出,即第一个实例。

My problem is that I am only getting "The Value of i is :0" as the output, i.e. the first instance.

任何帮助将是非常美联社preciated

Any help would be much appreciated

推荐答案

您不需要包装其他的LinearLayout内TextView的,你可以做:

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);
}

这篇关于用一个for循环TextViews创建线性布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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