首页 > 解决方案 > 如何用软键盘抬起按钮并能够滚动后面的其余内容

问题描述

我有一个Button在屏幕底部对齐的。打开时它会Button升起SoftKeyboard。页面中间是空白的。但它在顶部有一些内容,在底部对齐的正上方有一些内容Button。问题是当SoftKeyboard打开时,中间的空白区域被底部的内容填充了。相反,我想要的是,底部内容保持在原来的位置并能够向后滚动以查看它。 在此处输入图像描述

在此处输入图像描述

标签: android

解决方案


对于面临同样问题的人,我是这样解决的

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <!--data binding declaration-->
    </data>

    <RelativeLayout
        android:id="@+id/register_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/primary_amber_200"
        tools:context=".login.view.EnterEmailActivity">

        <LinearLayout
            android:id="@+id/register_ll"
            android:layout_width="match_parent"
            android:layout_height="92dp"
            android:orientation="vertical"

            android:layout_alignParentBottom="true"
            android:gravity="center">
            <View
                android:id="@+id/register_bar"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/primary_amber_100"
                android:layout_marginBottom="12dp"
                android:visibility="invisible"/>
            <Button
                android:id="@+id/register_loginBtn"
                style="@style/Button_Default_Dark_Grey"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin_small"
                android:fitsSystemWindows="true"
                android:text="@string/next"
                android:layout_marginLeft="@dimen/margin_medium"
                android:layout_marginRight="@dimen/margin_medium"/>

        </LinearLayout>


        <androidx.appcompat.widget.Toolbar
            style="@style/ToolBar_Basic_Light"
            android:background="@color/primary_amber_200"
            android:elevation="0dp"
            android:paddingLeft="@dimen/margin_medium"
            android:paddingRight="@dimen/margin_medium">

            <TextView
                android:id="@+id/toolbar_title"
                style="@style/Text_Basic_Toolbar" />
        </androidx.appcompat.widget.Toolbar>

        <ScrollView
            android:id="@+id/register_sv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_below="@id/toolbar"
            android:paddingLeft="@dimen/margin_medium"
            android:paddingRight="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_xlarge92">

            <RelativeLayout
                android:id="@+id/register_rl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/register_txtHeader"
                    style="@style/Text_Title_H1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_large"
                    android:text="@string/enter_email" />

                <EditText
                    android:id="@+id/register_email"
                    android:layout_width="match_parent"
                    android:layout_height="56dp"
                    android:layout_below="@id/register_txtHeader"
                    android:layout_marginTop="@dimen/margin_xlarge"
                    android:background="@color/primary_amber_100"
                    android:gravity="center_vertical"
                    android:hint="@string/email"
                    android:inputType="textEmailAddress"
                    android:paddingLeft="@dimen/margin_medium"
                    android:paddingRight="@dimen/margin_small" />

                <View
                    android:id="@+id/register_blank"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:layout_below="@id/register_email"
                    />

                <RelativeLayout
                    android:id="@+id/register_bottom_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/margin_small"
                    android:layout_below="@id/register_blank"
                    android:layout_marginTop="@dimen/margin_xxxlarge"
                    android:gravity="bottom"
                    android:layout_alignParentBottom="true">

                    <TextView
                        android:id="@+id/register_agreetxt"
                        style="@style/Text_Body_2"
                        android:textColor="@color/secondary_grey_800"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="bottom"
                        android:text="@string/agree" />

                    <TextView
                        android:id="@+id/register_terms"
                        style="@style/Text_Button_Normal"
                        android:layout_width="wrap_content"
                        android:layout_height="48dp"
                        android:layout_below="@id/register_agreetxt"
                        android:text="@string/terms"
                        android:gravity="center"/>

                    <TextView
                        android:id="@+id/register_policy"
                        style="@style/Text_Button_Normal"
                        android:layout_width="wrap_content"
                        android:layout_height="48dp"
                        android:layout_below="@id/register_agreetxt"
                        android:layout_marginLeft="@dimen/margin_xlarge"
                        android:layout_toRightOf="@id/register_terms"
                        android:text="@string/policy"
                        android:gravity="center_vertical"/>

                    <TextView
                        android:id="@+id/register_have_accnt"
                        style="@style/Text_Body_2"
                        android:textColor="@color/secondary_grey_800"
                        android:layout_width="wrap_content"
                        android:layout_height="48dp"
                        android:layout_below="@id/register_terms"
                        android:text="@string/already_have_an_account"
                        android:gravity="center"/>

                    <TextView
                        android:id="@+id/register_login"
                        style="@style/Text_Button_Normal"
                        android:layout_width="wrap_content"
                        android:layout_height="48dp"
                        android:layout_below="@id/register_terms"
                        android:layout_marginLeft="@dimen/margin_xlarge"
                        android:layout_toRightOf="@id/register_have_accnt"
                        android:text="@string/login"
                        android:gravity="center"/>
                </RelativeLayout>

            </RelativeLayout>


        </ScrollView>

    </RelativeLayout>
</layout>

不要忘记android:windowSoftInputMode="stateVisible|adjustResize"在清单文件中添加您的活动。


推荐阅读