You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android CoordinatorLayout下windowSoftInputMode设置失效问题求助

解决Android键盘弹出时底部按钮被遮挡的问题

看起来你遇到的问题是键盘弹出时布局没有正确适配,导致底部按钮被挡住。虽然你已经设置了adjustPan|adjustResize和fitsSystemWindows,但几个配置冲突和布局约束的问题导致没生效,下面是具体的解决方案:

1. 修正Manifest的windowSoftInputMode设置

同时使用adjustPan和adjustResize会让系统优先执行adjustPan(平移布局),而不会真正调整布局的大小,这就是底部按钮被挡住的核心原因之一。把你的Activity标签里的设置改成:

android:windowSoftInputMode="adjustResize"

2. 调整MainActivity的CoordinatorLayout配置

你的主布局用了CoordinatorLayout,并且给NavHostFragment设置了app:layout_behavior="@string/appbar_scrolling_view_behavior"——这个Behavior是配合AppBarLayout使用的,如果你当前布局里没有AppBarLayout,这个Behavior会干扰adjustResize的正常工作。修改你的MainActivity布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".ui.MainActivity" 
    android:fitsSystemWindows="true" 
    android:id="@+id/rootLayout">

    <fragment 
        android:id="@+id/nav_host_fragment" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:name="androidx.navigation.fragment.NavHostFragment" 
        app:navGraph="@navigation/mobile_navigation" 
        app:defaultNavHost="true" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

(移除了app:layout_behavior属性)

3. 优化Fragment的布局约束和配置

你的Fragment里的ConstraintLayout设置了android:layout_height="match_parent",同时ScrollView开启了fillViewport="true",这会让ConstraintLayout强制填充ScrollView的高度,导致键盘弹出时ScrollView无法正确收缩调整。另外,重复设置fitsSystemWindows也可能导致冲突,具体修改如下:

修改后的Fragment布局:

<ScrollView 
    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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:id="@+id/scrollView2" 
    android:fitsSystemWindows="true">

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" <!-- 改为wrap_content,让ScrollView可调整 -->
        tools:context=".ui.LoginByUserNameFragment" 
        android:layoutDirection="rtl"> <!-- 移除重复的fitsSystemWindows -->

        <!-- 保留你原有的返回箭头、标题图、短信登录按钮、分割线等视图 -->

        <com.google.android.material.textfield.TextInputLayout 
            android:id="@+id/edtPhon_input" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:hint="@string/prompt_phone" 
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 
            app:layout_constraintTop_toBottomOf="@+id/textView2" 
            android:layout_marginTop="28dp" 
            android:layout_marginEnd="8dp" 
            app:layout_constraintEnd_toEndOf="parent" 
            android:layout_marginStart="8dp" 
            app:layout_constraintStart_toStartOf="parent" >
            <com.google.android.material.textfield.TextInputEditText 
                android:id="@+id/edtPhon" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:inputType="phone" 
                android:maxLines="1" 
                android:background="@drawable/drw_bg_edt" 
                android:padding="@dimen/padding_button" 
                app:drawableLeftCompat="@drawable/phone_drawable_selector" 
                android:drawablePadding="3dp" 
                android:imeOptions="actionNext" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout 
            android:id="@+id/edtPassword_input" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" 
            app:layout_constraintTop_toBottomOf="@+id/edtPhon_input" 
            android:layout_marginStart="8dp" 
            app:layout_constraintStart_toStartOf="parent" 
            android:layout_marginEnd="8dp" 
            app:layout_constraintEnd_toEndOf="parent" 
            android:layout_marginTop="20dp">
            <com.google.android.material.textfield.TextInputEditText 
                android:id="@+id/edtPassword" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:inputType="textPassword" 
                android:maxLines="1" 
                android:background="@drawable/drw_bg_edt" 
                android:padding="@dimen/padding_button" 
                app:drawableLeftCompat="@drawable/pass_drw" 
                android:drawablePadding="3dp" 
                android:imeOptions="actionDone"/>
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.button.MaterialButton 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            app:layout_constraintTop_toBottomOf="@+id/edtPassword_input" 
            app:layout_constraintStart_toStartOf="parent" 
            android:layout_marginStart="8dp" 
            android:layout_marginEnd="8dp" 
            app:layout_constraintEnd_toEndOf="parent" 
            android:layout_marginTop="20dp" 
            android:textColor="@color/White" 
            android:text="@string/general_login" 
            android:gravity="center" 
            app:cornerRadius="5dp" 
            app:backgroundTint="@color/back_color_sign_btn" 
            android:enabled="false" 
            android:padding="@dimen/padding_button" 
            android:id="@+id/btn_login" 
            android:layout_marginBottom="16dp"/> <!-- 移除bottom约束,让按钮跟随输入框位置 -->

        <TextView 
            android:text="@string/not_member" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/txtMember" 
            android:background="?attr/selectableItemBackground" 
            android:paddingEnd="5dp" 
            android:paddingRight="5dp" 
            android:textColor="@color/generalGrey" 
            app:layout_constraintStart_toStartOf="parent" 
            app:layout_constraintHorizontal_bias="0.5" 
            app:layout_constraintEnd_toStartOf="@+id/txtSignUp" 
            app:layout_constraintTop_toBottomOf="@+id/btn_login" 
            app:layout_constraintHorizontal_chainStyle="packed" 
            android:layout_marginTop="16dp" 
            android:layout_marginBottom="32dp"/> <!-- 增加底部margin避免被导航栏遮挡 -->

        <TextView 
            android:text="@string/do_register" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:id="@+id/txtSignUp" 
            android:textColor="@color/mainGreen" 
            app:layout_constraintEnd_toEndOf="parent" 
            app:layout_constraintStart_toEndOf="@+id/txtMember" 
            app:layout_constraintHorizontal_bias="0.5" 
            app:layout_constraintTop_toBottomOf="@+id/btn_login" 
            android:layout_marginTop="16dp" 
            android:layout_marginBottom="32dp"/> <!-- 同样增加底部margin -->

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

关键修改点说明:

  • 将ConstraintLayout的android:layout_height从match_parent改为wrap_content,这样ScrollView可以根据内容高度动态调整,键盘弹出时ScrollView会收缩,内容可滚动到底部按钮。
  • 移除ConstraintLayout上重复的android:fitsSystemWindows="true",避免和ScrollView的设置冲突。
  • 去掉底部按钮btn_login的app:layout_constraintBottom_toBottomOf="parent"约束,让它跟随输入框的位置,同时给底部TextView增加足够的layout_marginBottom,确保不会被系统导航栏或键盘遮挡。

完成这些修改后,键盘弹出时,ScrollView会自动调整高度,你可以通过滚动看到底部的登录按钮和注册文本,不会被键盘挡住。

内容的提问来源于stack exchange,提问作者maryam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:16:01