【问题标题】:How to prevent soft keyboard from pushing up only the toolbar?如何防止软键盘只向上推工具栏?
【发布时间】:2015-12-18 18:53:56
【问题描述】:

我有一个布局,顶部有一个工具栏和几个 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打开软键盘时,整个屏幕(包含工具栏)将被向上推。除了工具栏(修复工具栏),有什么方法可以让软键盘上推整个屏幕?

现有的一些answers实际上可以固定整个屏幕,但是靠近底部的EditText也可能会被软键盘挡住。

谁能给我一个好的解决方案?

【问题讨论】:

    标签: android android-layout android-softkeyboard


    【解决方案1】:

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

    【讨论】:

    • 感谢您的帖子,我试过了,但现在没有达到我的预期。也许你带领我走上正轨,我会继续努力。
    • 为我工作,谢谢!你知道我是否可以让 adjuctPan 行为适应特定的布局或视图?而不是在清单中将其定义为整个应用程序。因为有些地方我宁愿使用adjustResize
    • 您可以为每个活动定义adjustPan,您不需要为整个应用程序定义它。
    • 这对我不起作用。 adjustResize 做到了
    【解决方案2】:

    使用 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>
    

    【讨论】:

    • 这不起作用仍然无法在打开键盘的情况下滚动视图
    猜你喜欢
    • 1970-01-01
    • 2011-05-11
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    相关资源
    最近更新 更多