0

我正在尝试以编程方式构建 Android 视图的一部分,但不幸的是我在使用RelativeLayout. 它让我Elements互相叠加

这是我的代码:

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);

这是它的样子:

在javacode中输出

如您所见,“描述”位于“分数”之上。

这是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 输出

如您所见,在 xml 中,toRightOf-command 按预期工作 - 描述位于分数的右侧

这怎么可能?不应该线

lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());

做同样的事?

4

1 回答 1

1

At the moment you are calling it, score.getId() will return -1 (invalid), because it has not been added yet to the screen with the whole layout processed. You will have to set the id manually with setId() before calling getId() and it should all work fine.

于 2012-03-11T12:45:20.203 回答