Android 文本视图和编辑文本对齐问题

Android 文本视图和编辑文本对齐问题,android,alignment,android-edittext,textview,Android,Alignment,Android Edittext,Textview,我正在将TextViews和editText添加到UI中,但它们彼此重叠。我希望他们出现在一起。我在这段代码中遗漏了什么 ScrollView sv = new ScrollView(this); RelativeLayout ll = new RelativeLayout(this); ll.setId(99); sv.addView(ll, new LayoutParams(LayoutParams.FILL_PAREN

我正在将
TextViews
editText
添加到UI中,但它们彼此重叠。我希望他们出现在一起。我在这段代码中遗漏了什么

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);
ScrollView sv=新的ScrollView(此);
RelativeLayout ll=新的RelativeLayout(本);
ll.setId(99);
sv.addView(ll,新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
ll.setBackgroundResource(R.drawable.background);
对于(int i=0;i<10/*更改了实际值以便更好地理解*/;i++){
tv=新文本视图(此);
tv.setText(“+(productStr[i]);
tv.setId(i);
tv.setTextSize(TypedValue.COMPLEX\u UNIT\u PX,18);
RelativeLayout.LayoutParams lay=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
lay.addRule(RelativeLayout.ALIGN_RIGHT,RelativeLayout.TRUE);
ll.addView(电视、播放);
et=新编辑文本(本);
et.setInputType(InputType.TYPE\类别\编号);
et.setEms(2);
添加(et);
et.setId(i);
RelativeLayout.LayoutParams p=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
p、 addRule(RelativeLayout.ALIGN_BOTTOM,tv.getId());
ll.addView(et,p);
}
这个.setContentView(sv);

使用LinearLayout而不是RelativeLayout,并将定位留给layouter


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

只需在xml中使用
线性布局即可。类似的操作将在
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>


附言:在这个论坛上,每个人提问时都会遵循一些规范,比如在你的问题中不使用聊天语言。写一个简短而甜美的标题,你可以在你的文章中写任何数量的解释。

没有必要添加额外的版面,你可以使用
RelativeLayout
。下面的代码应该按照您的需要布局
文本视图
编辑文本

        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);
ScrollView sv=新的ScrollView(此);
RelativeLayout ll=新的RelativeLayout(本);
ll.setId(99);
ll.setBackgroundResource(R.drawable.background);
sv.addView(ll,新LayoutParams(LayoutParams.FILL_父级,
LayoutParams.FILL_PARENT));
对于(int i=0;i<10;i++){
tv=新文本视图(此);
tv.setText(“+(productStr[i]);
tv.setId(1000+i);
tv.setTextSize(TypedValue.COMPLEX\u UNIT\u PX,18);
RelativeLayout.LayoutParams lay=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
lay.addRule(RelativeLayout.ALIGN\u PARENT\u LEFT,RelativeLayout.TRUE);
如果(i!=0){
lay.addRule(RelativeLayout.down,2000+i-1);
}
ll.addView(电视、播放);
et=新编辑文本(本);
et.setInputType(InputType.TYPE\类别\编号);
et.setEms(2);
et.setId(2000+i);
RelativeLayout.LayoutParams p=新的RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_内容,
RelativeLayout.LayoutParams.WRAP_内容);
p、 addRule(RelativeLayout.RIGHT_OF,tv.getId());
如果(i!=0){
p、 addRule(RelativeLayout.down,2000+i-1);
}
ll.addView(et,p);
}
这个.setContentView(sv);

smart guy(WebnetMobile.com)。。。老兄,如果你能回答我的要求,我会非常感激你的。。。。希望您知道标题中有大约150个字符,您必须使用标题进行管理。对不起,WebnetMobile.com。。上次有点粗鲁。他有点紧张。将尝试不再使用聊天语言:-)没问题。坦白地说,如果我知道150个字符的限制,我可能根本不会评论“dem”。所以我们是平方:)对于(inti=0;i<10;i++{tv=(TextView)findViewById(R.id.TextView 1);tv.setText(“+(productStr[i]);et=(EditText)findViewById(R.id.editText1);allEds.add(et);}嗨,Abhilasha。。。我在上面用你建议的xml进行了尝试,效果很好,但屏幕上只有一个TextView和EditText,这也是循环的最后一个。请帮我完成这项工作。for(int i=0;i<10;i++){tv=(TextView)findViewById(R.id.textView1);tv.setText(“+(productStr[i]);et=(EditText)findViewById(R.id.editText1);allEds.add(et);}你好,阿比拉沙。。。我在上面尝试了您建议的xml,它工作正常,但屏幕上只有一个TextView和EditText,这也是循环的最后一个。请帮我完成这项工作。您需要多少个
TextView
s和
EditText
s?您必须在Layout.xml文件中定义它们中的许多。我已经编辑了我的答案,如果您需要两行
TextView
EditText
文本框和EditText的数量在我的项目中没有限制,因为它可能是10或20,这取决于要求。不管怎样,我已经解决了这个问题,而且效果很好谢谢阿比拉沙的关心。。非常感谢…:-)谢谢你。我尝试了解决方案,但它导致我的应用程序强制关闭:-(@user1531689我的代码在我测试时工作,因此错误一定来自代码的另一部分。你能在某个地方发布(比如)您从logcat获得的异常?@Luksprog,我已经按照您的要求将异常粘贴到pestbin中。@user1531689那么您必须给我链接才能看到它。@Luksprog tha