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


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

帖子1:

TextViewUSer TextViewDate

TextViewMessage

帖子2:

TextViewUSer TextViewDate

TextViewMessage

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

我也用addRules .ABOVE和其他代码实现了,但这甚至更糟,因为它们卡在了左上角

最佳答案

您可以像这样将“规则”添加到文本视图中

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


例如。

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)

10-04 20:02