TextView和EditText对齐问题

时间:2012-07-17 12:06:45

标签: android alignment android-edittext textview

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

只需在xml中使用LinearLayout即可。这样的内容会在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>
P.S:在这个论坛上提问时,每个人都遵循的规范很少,比如在你的问题中没有使用聊天语言。制作简短的标题,你可以在帖子中写下任何数量的解释。

答案 2 :(得分:0)

无需添加额外的布局,您可以使用RelativeLayout执行此操作。下面的代码应该按照您的要求布置TextViewEditText

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