0
我試圖以編程方式構建Android視圖的一部分,但不幸的是,我在RelativeLayout
中遇到了一些問題。它使我的Elements
上海誓山盟RelativeLayout元素不正確重疊
覆蓋這是我的代碼:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_layout);
LinearLayout ll = (LinearLayout) findViewById(R.id.container);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
// fill content
for (int i = 0; i < 3; i++) {
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView score = new TextView(this);
score.setText(""+i);
RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
score.setLayoutParams(lpScore);
TextView description = new TextView(this);
description.setText("this is my description");
RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
description.setLayoutParams(lpDescription);
CheckBox checkbox = new CheckBox(this);
RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(score);
rl.addView(description);
rl.addView(checkbox);
tl.addView(rl);
}
ll.addView(tl);
這是什麼樣子:
正如你所看到的,「說明」是對「得分」的頂部。
這裏是XML格式的同一代碼:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tableLayoutTextEntry" >
<TableRow
android:id="@+id/tableRowTextEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="score" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/score"
android:text="description" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="select" />
</RelativeLayout>
</TableRow>
</TableLayout>
這裏是什麼樣子的XML:
正如你可以看到,在XML中,toRightOf命令按預期工作 - 描述是在得分右側
這怎麼可能?不應該行
lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());
做同樣的事情?
您是否需要在代碼中創建佈局? – Egor 2012-03-11 12:40:56
@Egor是的,即使他可以通過定義**自定義適配器ListView ** – 2012-03-11 12:45:25