Android RelativeLayout元素的重叠不正确

Android RelativeLayout元素的重叠不正确,android,android-relativelayout,overlays,Android,Android Relativelayout,Overlays,我试图以编程方式构建Android视图的一部分,但不幸的是,RelativeLayout出现了一些问题。它使我的元素相互重叠 这是我的代码: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.insertnew_layout); LinearLayout ll = (LinearLayout) find

我试图以编程方式构建Android视图的一部分,但不幸的是,
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);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.insertnew_布局);
LinearLayout ll=(LinearLayout)findViewById(R.id.container);
ll.设置方向(线性布局、垂直);
TableLayout tl=新的TableLayout(本);
tl.setLayoutParams(新的LayoutParams(LayoutParams.FILL_父级,LayoutParams.FILL_父级));
//填充内容
对于(int i=0;i<3;i++){
TableRow tr=新的TableRow(本);
tr.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
RelativeLayout rl=新的RelativeLayout(本);
rl.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
TextView分数=新的TextView(本);
score.setText(“+i”);
RelativeLayout.LayoutParams lpScore=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.FILL_父项);
lpScore.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT);
score.setLayoutParams(lpScore);
TextView description=新的TextView(此);
description.setText(“这是我的描述”);
RelativeLayout.LayoutParams lpDescription=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.FILL_父项);
lpDescription.addRule(RelativeLayout.RIGHT_OF,score.getId());
description.setLayoutParams(lpDescription);
复选框=新复选框(此复选框);
RelativeLayout.LayoutParams lpCheckbox=新建RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.FILL_父项);
lpCheckbox.addRule(RelativeLayout.ALIGN\u PARENT\u RIGHT);
checkbox.setLayoutParams(lpCheckbox);
rl.addView(分数);
rl.addView(说明);
rl.addView(复选框);
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());


做同样的事情吗?

在调用它时,
score.getId()
将返回-1(无效),因为它还没有被添加到整个布局处理的屏幕中。
在调用
getId()
之前,您必须使用
setId()
手动设置id,这一切都应该可以正常工作。

是否需要在代码中创建布局?@Egor是的,甚至他也可以通过为ListView定义自定义适配器来轻松实现这一点