如何防止软键盘仅推起工具栏?

4

我有一个布局,顶部有一个工具栏和几个EditText视图:

<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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/my_toolbar">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text1"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text2"/>

            ...

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text7"/>
    </LinearLayout>

</RelativeLayout>

当一些靠近屏幕底部的EditText打开软键盘时,整个屏幕(包括工具栏)都将被推上去。有没有办法让软键盘推起整个屏幕,除了工具栏(固定工具栏)?

一些现有的答案实际上可以修复整个屏幕,但靠近底部的EditText也可能被软键盘遮挡。

有人能给我一个好的解决方案吗?

2个回答

4
使用ScrollView。
<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">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
</android.support.v7.widget.Toolbar>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/my_toolbar">

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

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text1"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text2"/>

            ...

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_text7"/>
    </LinearLayout>

</ScrollView>


1
仍然无法正常工作,键盘打开时无法滚动视图。 - Erum

4
在第一个相对布局中加入android:fitsSystemWindows="true"。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
....
....
</RelativeLayout>

在清单文件中添加以下内容:

android:windowSoftInputMode="adjustPan"

谢谢您的帖子,我尝试了但是目前它并没有按照我的期望工作。也许您给了我正确的方向,我会继续努力的。 - Rayliu521
对我有用,谢谢! 你知道我是否可以将adjustPan行为设置为特定的布局或视图?而不是在清单中定义整个应用程序。因为有些地方我更愿意使用adjustResize。 - BVtp
你可以为每个活动定义adjustPan,无需为整个应用程序定义它。 - Deepak
1
这对我没有用。adjustResize却可以。 - Vucko

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