0

我正在向 UI 添加TextViewsEditTexts,但它们相互重叠。我希望它们彼此相邻出现。我在这段代码中缺少什么?

ScrollView sv = new ScrollView(this);
            RelativeLayout ll = new RelativeLayout(this);
            ll.setId(99);
            sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            ll.setBackgroundResource(R.drawable.background);

        for (int i = 0; i < 10 /*Changed the actual value for better Understanding*/; i++) {
            tv = new TextView(this);
            tv.setText("" + (productStr[i]));
            tv.setId(i);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
            RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lay.addRule(RelativeLayout.ALIGN_RIGHT, RelativeLayout.TRUE);

            ll.addView(tv, lay);
            et = new EditText(this);
            et.setInputType(InputType.TYPE_CLASS_NUMBER);
            et.setEms(2);
            allEds.add(et);
            et.setId(i);

             RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                     RelativeLayout.LayoutParams.WRAP_CONTENT);
             p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
            ll.addView(et, p);


        }
        this.setContentView(sv);
4

3 回答 3

1

使用 LinearLayout 而不是 RelativeLayout,并将定位留给布局器。

PS:拼写为“them”而不是“dem”。

于 2012-07-17T12:12:39.983 回答
0

只需LinearLayout在您的 xml 中使用。像这样的东西会在TextView旁边生成一个EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

        </EditText>
    </LinearLayout>

</LinearLayout>

PS:每个人在这个论坛上提问时都遵循一些规范,比如在你的问题中不使用聊天语言。制作简短而甜美的标题,您可以在帖子中写下任意数量的解释。

于 2012-07-17T12:30:35.560 回答
0

无需添加额外的布局,您可以使用RelativeLayout. 下面的代码应该像你想要的那样布局TextView和:EditText

        ScrollView sv = new ScrollView(this);
        RelativeLayout ll = new RelativeLayout(this);
        ll.setId(99);
        ll.setBackgroundResource(R.drawable.background);

        sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        for (int i = 0; i < 10; i++) {
            tv = new TextView(this);
            tv.setText("" + (productStr[i]));
            tv.setId(1000 + i);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 18);
            RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lay.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            if (i != 0) {
                lay.addRule(RelativeLayout.BELOW, 2000 + i - 1);
            }
            ll.addView(tv, lay);
            et = new EditText(this);
            et.setInputType(InputType.TYPE_CLASS_NUMBER);
            et.setEms(2);
            et.setId(2000 + i);
            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.RIGHT_OF, tv.getId());
            if (i != 0) {
                p.addRule(RelativeLayout.BELOW, 2000 + i - 1);
            }
            ll.addView(et, p);
        }
        this.setContentView(sv);
于 2012-07-18T06:05:06.173 回答