0

我正在尝试将 textview 动态添加到 textView 的右侧,但 textView 未显示。

语句“ params2.addRule(RelativeLayout.RIGHT_OF, 1);” 如果我删除该行,则在下面的代码中会产生问题,textview 将显示在随机位置。

需要动态实现这个xml

<TextView
            android:id="@+id/stationnameVal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="KANYAKUMARI (CAPE)"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/arrivaltimeVal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="45dp"
            android:layout_toRightOf="@+id/stationnameVal"
            android:text="SRC"
            android:textColor="@color/black"
            android:textSize="12sp" />

代码是:

    RelativeLayout rel = new RelativeLayout(this);
    // first TextView
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        TextView stateName = new TextView(DisplayActivity.this);
        stateName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
        params.setMargins(0, 5, 0, 0);
        stateName.setText("KANYAKUMARI (CAPE)");
        stateName.setId(1);
        stateName.setTextColor(R.color.black);
        stateName.setTextSize(12);
        rel.addView(stateName, params);

    // Adding Second TextView
       RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

       TextView arr = new TextView(DisplayActivity.this);
       arr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
       arr.setText("SRC");
       arr.setTextColor(R.color.white);
       arr.setTextSize(12);
       params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());
       params2.addRule(RelativeLayout.RIGHT_OF, 1);
       params2.setMargins(45, 0, 0, 0);
       rel.addView(arr, params2);
4

2 回答 2

1

change this:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

or this:

rel.addView(stateName, params);

to

rel.addView(stateName);

and also change this:

params2.addRule(RelativeLayout.ALIGN_PARENT_TOP, stateName.getId());

to

params2.addRule(RelativeLayout.ALIGN_PARENT_TOP);

and do the same for param:

params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

otherwise remove it, because Kanyakumari and src are not aligned properly.

actually do this to align them:

params2.addRule(RelativeLayout.ALIGN_TOP, 1);
于 2012-08-17T07:34:02.133 回答
1

将“LayoutParams.FILL_PARENT”更改为“LayoutParams.WRAP_CONTENT”怎么样?

问候

陈子腾

于 2012-08-17T07:32:03.057 回答