【问题标题】:Two edit text in same line, without overlapping同一行中的两个编辑文本,不重叠
【发布时间】:2014-02-27 19:58:39
【问题描述】:

我有 2 个 EditText et1 & et2。

它们都将显示在同一行,即在彼此的前面。 et1 是alignedParentLeft et2 是alignedParentRight

而且,对于android:singleLine="true"

问题是,如果一个的长度覆盖了屏幕的长度,那么它就会与另一个重叠。

如何确保两者都可见。 没关系,如果尺寸更大,而不是重叠,显示椭圆(最后是'...')。

【问题讨论】:

    标签: android


    【解决方案1】:

    在编辑文本中赋予权重。

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:ems="10" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您可以简单地将LinearLayoutandroid:orientation="horizontal" 一起使用,例如:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
          <EditText />
          <EditText />
      </LinearLayout>
      

      【讨论】:

        【解决方案3】:

        将此行添加到et2的xml中:

        android:toRightOf="@+id/et1"
        

        这确保 et2 将始终位于 et1 的右侧,因此它们不会重叠。

        【讨论】:

          【解决方案4】:

          我找到的解决方案是使用android:layout_width:0dp

          在优先级较低的编辑文本之一中使用此宽度。当具有较高优先级的文本变得比第二个更大时,而不是重叠离开屏幕,

          这不是我需要的 100%,但对我有用。

          谢谢

          【讨论】:

            【解决方案5】:
            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="2" >
            
                <EditText
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="Edittext1" />
            
                <EditText
                    android:id="@+id/email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="Edittext2" />
            
            </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-07-19
              • 2016-06-29
              • 1970-01-01
              相关资源
              最近更新 更多