动态的TextView中相对布局布局、动态、TextView

2023-09-04 09:45:13 作者:要活的比阳光还更灿烂

我triying用于我的项目注释部分的动态布局,但是当我的setText的TextView的动态地输出仅出现在屏幕的上方。而且它把输出过其它输出

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs

RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
        for(int i = 0; i < 20; i++) {
        TextView cb = new TextView(this);
        cb.setText("YORUMLAR"+yorum[0]+i);

         cb.setTextSize(30);
          ll.addView(cb); 

        }

那么,怎样才能把我的输出屏幕线性的底部。

So how can i put the output on the bottom of the screen linearly.

推荐答案

您应该使用的LinearLayout 自动添加一个的TextView 层出不穷。

You should use LinearLayout to automatically add one TextView after another.

假设你的生活不能没有 RelativeLayout的,你需要动态生成的ID为所有的TextView 您创建为了把一个视图下另一个。下面是例子:

Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:

public class HelloWorld extends Activity
{       
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);

        Random rnd = new Random();
        int prevTextViewId = 0;     
        for(int i = 0; i < 10; i++)
        {                       
            final TextView textView = new TextView(this);
            textView.setText("Text "+i);     
            textView.setTextColor(rnd.nextInt() | 0xff000000);            

            int curTextViewId = prevTextViewId + 1;
            textView.setId(curTextViewId);
            final RelativeLayout.LayoutParams params = 
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
                                                RelativeLayout.LayoutParams.WRAP_CONTENT);

            params.addRule(RelativeLayout.BELOW, prevTextViewId);
            textView.setLayoutParams(params);

            prevTextViewId = curTextViewId;
            layout.addView(textView, params);
        }              
    }    
}