【问题标题】:EditText overflowing into buttonEditText 溢出到按钮
【发布时间】:2015-03-28 10:28:31
【问题描述】:

我的片段活动中有一个EditText 和一个Button

我面临两个问题。

1) 我不能将Button 放在EditText 的正下方。

这里是截图:

2) 每当我在EditText 中粘贴冗长的文本时,按钮都会与文本重叠。请看截图。

这是我的 XML:

      <FrameLayout>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:text="@string/URL"
            android:hint="@string/hint"
            android:layout_marginTop="200dp"
            android:layout_gravity="center_horizontal|top" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Track"
            android:id="@+id/button"
            android:layout_gravity="center"
            />

       </FrameLayout>

如何解决问题。有什么帮助吗?

【问题讨论】:

    标签: android xml android-layout android-edittext android-button


    【解决方案1】:

    您可能要考虑使用RelativeLayout,而不是使用FrameLayout。然后您可以执行以下操作:

    对于 EditText,使用以下属性:

    // This says the widget should be vertically and horizontally at the center of the container
    android:layout_centerInParent="true"
    

    对于按钮,使用以下属性:

    android:layout_below:"@id/editText"
    

    我提到的属性将替换

    android:layout_gravity

    您使用的两个小部件的属性。

    EditText 也会丢失

    android:layout_marginTop="200dp"

    作为 layout_centerInParent 属性的属性会自动居中。

    【讨论】:

    • 谢谢@Sunil。有效。两个问题都解决了。我还应该在 中使用 吗?
    • 不,你不应该。 LinearLayout 在“长子获胜”的方法中遵循自己的设计原则。在 RelativeLayout 中,后面的孩子在 Z 轴上更高 - 这有它自己的模式。对于您的目的,RelativeLayout 绰绰有余。
    【解决方案2】:

    使用RelativeLayout 或LinearLayout 代替FrameLayout。 FrameLayout 的目的是显示一个项。

    【讨论】:

      【解决方案3】:

      这是伪代码,但可以帮助您理解结构

      <RelativeLayout>
      
          <LinearLayout
          orientation=vertical
          centerInParent= true
          >
      
             <EditText/>
             <Button/>
          </LinearLayout>
      
       </RelativeLayout>
      

      【讨论】: