我正在尝试将 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);