简体   繁体   English

Android:如何使用下面的按钮来布局全屏EditText?

[英]Android: how to layout a fullscreen EditText, with a button below?

This seems simple, but I can't get it to work. 这似乎很简单,但是我无法使其正常工作。 I'd like to place a button at the bottom of the screen, and have an EditText occupy the rest of the available space. 我想在屏幕底部放置一个按钮,并让EditText占据剩余的可用空间。 The below layout shows creates an EditText that occupies the full screen but the Button is not visible. 下面的布局显示了创建一个EditText,该EditText占据了整个屏幕,但是Button不可见。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/textField"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </EditText>
    <Button
        android:id="@+id/button"
        android:text="text"
        android:layout_below="@id/textField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </Button>

</RelativeLayout>

Try the following inside your RelativeLayout: 在您的RelativeLayout中尝试以下操作:

<EditText
    android:id="@+id/textField"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button" />

<Button
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

The problem with your original layout is that your EditText's height was set to match_parent and your button was set to be below that. 原始布局的问题是EditText的高度设置为match_parent而按钮设置为低于该高度。 match_parent caused the EditText to take up the full height of the screen, pushing the button below the bottom of the screen. match_parent导致EditText占据屏幕的整个高度,按下屏幕底部下方的按钮。

By telling the button to align itself to the bottom of the screen, then telling the EditText to lay itself out above that, you should be able to achieve what you are looking for. 通过让按钮使自己与屏幕底部对齐,然后让EditText放置在屏幕上方,您应该能够实现所需的功能。

It is because of layout, you need to use the right layout. 由于布局原因,您需要使用正确的布局。 For example linear layout could help much more in this situation. 例如,线性布局可以在这种情况下提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM