当键盘弹出时如何将整个布局向上移动?

21
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_gravity="bottom"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" /> 
        </LinearLayout>    

</FrameLayout>  

在清单文件中:

 android:windowSoftInputMode="adjustPan"

初始行为:

enter image description here

当前行为:

enter image description here

期望行为:

enter image description here

如何实现所需的布局行为?因为选项adjustResize会调整布局大小,所以不适合使用。

感谢您的回答!


我认为你应该使用滚动视图作为主要的父级布局。 - Jamil
抱歉,我不明白你的意图。假设在文本视图上方和下方还有另外两个按钮;你希望的行为是什么?如果所有内容都向上移动,那这不就是adjustResize吗? - Anudeep Bulla
9个回答

2

在您的清单文件中尝试以下内容:

android:windowSoftInputMode="adjustResize|stateUnchanged"

2
唯一能够获得这个结果的方法是调整布局大小。 使用android:windowSoftInputMode="adjustResize"。

1
adjustResize不适合我,因为此选项会调整布局大小。 - Sinigami

2
你可能想要查看这篇回答:此处。其中有一个建议:
将窗口的SoftInput Mode属性设置为adjustPan和adjustResize。
<activity android:windowSoftInputMode="adjustPan|adjustResize"> </activity>

1
尝试将您的LinearLayout中的FrameLayout更改为带有alignParentBottom="true"属性的RelativeLayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_alignParentBottom="true"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" /> 
        </LinearLayout>    

</RelativeLayout>  

1
你无法使用adjustPan来实现这一点,因为根据官方指南

活动的主窗口不会被调整大小以腾出空间给软键盘。相反,窗口的内容会自动平移,以便当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小好,因为用户可能需要关闭软键盘才能访问和与窗口中被遮挡的部分交互。

你的Button在当前焦点EditText下面。

1

1

检查包含EditText和Button的布局是否为RelativeLayout。


请尝试将 LinearLayout 替换为 RelativeLayout。 - Liran Peretz
尝试将框架布局替换为相对布局。 - Liran Peretz

1
我不相信有一种简单的方法来做到这一点。至少没有设置标志那么简单。 我有一个类似的情况,希望键盘能够上升到所选EditText下面的视图水平。最终我使用了这个答案: 如何检查Android中软键盘的可见性? 并在键盘出现时手动调整我的视图大小。

0

只需在活动中使用此属性

android:windowSoftInputMode="adjustPan"

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