在我的xml中我有:
<RelativeLayout
android:id="@+id/mainWeek"
android:layout_height="387dip"
android:layout_width="match_parent"
android:layout_marginTop="5dip">
<RelativeLayout
android:id="@+id/day1"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_height="50dip"
android:layout_width="match_parent">
...
</RelativeLayout>
</RelativeLayout>
在代码中我想要类似的东西:
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.BELOW, R.id.day1);
rlp.addRule(RelativeLayout.ALIGN_LEFT, R.id.day1);
TextView tv1 = new TextView(this);
tv1.setText("A");
layout.addView(tv1);
this.addContentView(layout, rlp);
但“布局”及其“tv1”出现在屏幕顶部而不是“day1”下方。 我究竟做错了什么? 感谢
答案 0 :(得分:0)
也许您确实希望将layout
视图添加到由RelativeLayout
标识的mainWeek
:
RelativeLayout mainWeek = (RelativeLayout) findViewById(R.id.mainWeek);
//Build the layout view object
mainWeek.addView(layout, rlp);
使用方法addContentView()
(我认为),您可以将布局视图直接附加到主窗口。