首页 文章

如何防止软键盘仅向上推?

提问于
浏览
3

我有一个布局,顶部有一个工具栏和几个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也可能被软键盘阻挡 .

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

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>
    
  • 2

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

相关问题