如何将按钮移动到键盘上方?

4
这个问题之前已经有人问过,但我找不到一个适合我的好答案。
以下是我的XML代码(已更新):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Account Name"
        android:textSize="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="15dp"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/accountName"
        android:background="@drawable/edit_text_background"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:padding="10dp"
        android:backgroundTint="@color/abc_secondary_text_material_light" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Account Balance"
        android:textSize="32dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="15dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:inputType="numberDecimal"
        android:id="@+id/accountBalance"
        android:background="@drawable/edit_text_background"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:padding="10dp"
        android:backgroundTint="@color/abc_secondary_text_material_light"
        android:text="£"
        />


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

我已经尝试使用android:windowSoftInputMode="adjustPan|adjustResize",但它并没有起作用。
我感觉我在XML代码的布局上做错了什么……它看起来像这样,当键盘出现时,我想让按钮向上移动:
请参考以下截图:enter image description here

将Xml代码放入ScrollView中。 - Arjun saini
这可能与 android:layout_alignParentBottom="true" 有关吗? - Mat Whiteside
@Apurva 即使使用相对布局而不是线性布局,并且删除了第二个相对布局,按钮仍然没有向上移动。这可能是因为使用了alignParentBottom吗? - Mat Whiteside
将按钮与parentBottom对齐后,您无法将按钮向上移动,否则会破坏布局。如果您希望在用户输入时显示按钮,则可以将按钮放置在EditText 2正下方。 - Gyanendra Mani
@GyanendraMani 有没有一种方法可以使按钮在底部对齐,但在显示键盘时将其向上移动? - Mat Whiteside
显示剩余6条评论
3个回答

4

尝试在父级别使用ScrollView

<ScrollView
            android:id="@+id/login_form"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

ScrollView 只能承载一个直接父级,因此它需要另一个布局在其下面... 我应该使用哪个? - Mat Whiteside
在那个滚动视图下,您可以使用一个线性布局。 - Uma Achanta

1
在你的 AndroidManifest.xml 文件中。
android:windowSoftInputMode="stateVisible|adjustResize"

在style.xml文件中,将您的活动样式设置为:
<item name="android:windowActionBarOverlay">true</item>

请确保您没有使用全屏活动。


-1

我曾经也为此苦恼了很久。对我有效的解决方案是在活动清单中添加以下行:

android:windowSoftInputMode="stateHidden"

我请求保留滚动视图内的布局,除了您希望随键盘移动的视图(在此问题中为按钮)。


这不是这个问题的答案。 - Kirguduck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接