【问题标题】:TextView and EditText alignment issuesTextView 和 EditText 对齐问题
【发布时间】:2012-07-16 08:26:45
【问题描述】:

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

【问题讨论】:

    标签: android alignment android-edittext textview


    【解决方案1】:

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

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

    【讨论】:

    • smart guy(WebnetMobile.com)...老兄,如果您回答我真正想要的,我一定会很感激您...希望您知道您拥有的标题大约有 150 个字符管理标题。
    • Sorry WebnetMobile.com.. 上次有点粗鲁。有点紧张。将尝试不再使用聊天语言:-))
    • 没问题。坦率地说,如果我知道 150 个字符的限制,我可能根本不会评论“dem”。所以我们是方形的:)
    【解决方案2】:

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

    <?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:每个人在此论坛上提问时都遵循一些规范,例如在您的问题中不使用聊天语言。制作简短而甜美的标题,您可以在帖子中写任意数量的解释。

    【讨论】:

    • for (int i = 0; i
    • for (int i = 0; i
    • 您需要多少个TextViews 和EditTexts?您必须在 Layout.xml 文件中定义其中的许多。对于需要两行 TextViewEditText 的情况,我已经编辑了我的答案
    • 文本框和 EditTexts 的数量在我的项目中不受限制,因为它可以是 10 或 20 取决于要求。无论如何,我已经解决了这个问题并且工作正常..感谢 Abhilasha 的关注..非常感谢..:-))
    【解决方案3】:

    不需要添加额外的布局,您可以使用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);
    

    【讨论】:

    • 感谢 Luksprog。我尝试了解决方案,但它导致我的应用程序强制关闭:-((
    • @user1531689 我的代码按照我的测试工作,因此错误必须来自代码的另一部分。您可以在某处(例如 pastebin.com )发布您从 logcat 获得的异常吗?
    • @Luksprog,我已按照您的要求将异常粘贴到害虫箱中。
    • @user1531689 你必须给我链接才能看到它。
    • @ Luksprog 谢谢老兄.. 我的应用程序现在运行良好.. 我的代码中有错误,我刚刚得到并删除了.. 非常感谢:-))