相对布局动态textView

时间:2013-07-08 09:51:10

标签: android relativelayout

 RelativeLayout layout = (RelativeLayout)findViewById(R.id.rl);
        for(int i = 1; i <= count; i++)
        {
            final TextView textViewUtente = new TextView(mContext);
            final TextView textViewMessaggio = new TextView(mContext);
            final TextView textViewData = new TextView(mContext);
            textViewUtente.setText(sepUser[i]);
            textViewMessaggio.setText(sepMessage[i]);
            textViewData.setText(sepDate[i]);
            int curTextViewIdUtente = 1000+i;
            int curTextViewIdMessaggio = 2000+i;
            int curTextViewIdData = 3000+i;
            textViewUtente.setId(curTextViewIdUtente);
            textViewMessaggio.setId(curTextViewIdMessaggio);
            textViewData.setId(curTextViewIdData);
            final RelativeLayout.LayoutParams params =
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);



            params.topMargin= 100+i*150;

            textViewUtente.setLayoutParams(params);
            layout.addView(textViewUtente, params);

            params.topMargin= 250+i*150;

            textViewMessaggio.setLayoutParams(params);
            layout.addView(textViewMessaggio, params);

            params.topMargin= 100+i*150;
            params.rightMargin=200;
            textViewData.setLayoutParams(params);
            layout.addView(textViewData, params);
        }

我需要创建3个textview(在用户的博客上创建帖子)每个for循环如下:

发布1:

TextViewUSer TextViewDate

TextViewMessage

帖子2:

TextViewUSer TextViewDate

TextViewMessage

我可以将一个博客的帖子(一组文本视图)放在另一个上面。 我可以像这样定位他们彼此。他们把一个放在另一个上面。

我还使用.ROVE和其他人的addRules,但它甚至是最糟糕的,因为它们全部位于左上角

1 个答案:

答案 0 :(得分:1)

您可以添加规则到您的文字视图

 tv1 = new TextView(this);
 tv1.setId((int)System.currentTimeMillis());

 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);                
 lp.addRule(RelativeLayout.BELOW, recent.getId());

 tv1.setText("Time: "+System.currentTimeMillis());
 rr.addView(tv1, lp);

EGS。

lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId())
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
相关问题