android - 相对布局动态textView

标签 android 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);
        }

我需要像这样在每个 for 循环中创建 3 个 TextView (从用户创建博客上的帖子):

帖子 1:

TextViewUSer TextViewDate

TextView 消息

帖子 2:

TextViewUSer TextViewDate

TextView 消息

我可以将一个博客的帖子(一组 TextView )置于另一个之上。 我可以像这样从彼此定位它们。他们把一个放在另一个上面。

我也对 addRules 和 .ABOVE 等进行了验证,但更糟糕的是它们都卡在了左上角

最佳答案

您可以像这样添加规则到您的 TextView

 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)

关于android - 相对布局动态textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17523678/

相关文章:

android - picasso 的 Target 和 centerCrop() 和 fit()

java - 如何同时设置随机颜色和按钮圆角?

android - android opennlp构建错误:无法访问找不到java.nio.file.Path的路径类文件

android - 最大化 ListView 中的可见行

php - PHP GET 方法中的多个请求

java - 网络游戏: update the clients with deltas or with the new status

android - 不显示 ScrollView 中 RelativeLayout 的最后一个 LinearLayout

android - 相对布局内的文本水平对齐方式

android - 在Android中定义RelativeLayout View 的Z顺序

android - 为标签 RelativeLayout 找到意外的命名空间前缀 "app"- Android?