Android中打开软键盘时CardView底部出现多余空白问题
解决Android登录页面软键盘弹出时CardView底部空白问题
问题根源
你的布局里有几个关键设置导致了该问题:
- CardView设置了
android:layout_height="0dp",但它的父布局是NestedScrollView,没有匹配的约束支撑这个高度,软键盘弹出时布局重绘就会出现多余空白。 - 多个布局重复设置
android:fitsSystemWindows="true",包括根布局、中间容器和CardView,这会让系统处理软键盘时的布局逻辑混乱。 - NestedScrollView的
fillViewport="true"配合CardView的0dp高度,会强制CardView填充视口,当内容高度不够时,底部就会出现空白。
修复步骤
直接修改以下几处:
- 修正CardView高度
把CardView的高度从0dp改成wrap_content,让它根据内部的输入框、按钮自适应高度:
<androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" app:cardCornerRadius="25dp" app:cardElevation="10dp" app:cardUseCompatPadding="true">
- 移除多余的fitsSystemWindows属性
删除constContainLogin和CardView上的android:fitsSystemWindows="true",只保留根ConstraintLayout的该属性,避免布局冲突:
<!-- 修改后的constContainLogin --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constContainLogin" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="15dp" android:orientation="vertical" android:background="@drawable/layout_bg" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtDescLogin">
- 调整NestedScrollView配置
移除NestedScrollView的android:fillViewport="true",避免强制填充视口导致的空白:
<androidx.core.widget.NestedScrollView android:scrollbars="none" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginEnd="30dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent">
完整修改后的布局(关键部分已修正)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLoginFragment" android:fitsSystemWindows="true" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/orangePeel_1" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" tools:context="ir.appinoo.presentation.ui.fragments.login.LoginFragment"> <TextView android:id="@+id/txtTitleLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_marginRight="20dp" android:fontFamily="@font/yekanbakh_regular" android:gravity="right" android:text="@string/login_title" android:textSize="20sp" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/txtDescLogin" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/txtDescLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:fontFamily="@font/yekanbakh_regular" android:gravity="right" android:text="@string/login_title_desc" android:textStyle="bold" app:layout_constraintBottom_toTopOf="@+id/constContainLogin" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtTitleLogin" /> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/constContainLogin" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="15dp" android:orientation="vertical" android:background="@drawable/layout_bg" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/txtDescLogin"> <androidx.core.widget.NestedScrollView android:scrollbars="none" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="30dp" android:layout_marginEnd="30dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" app:cardCornerRadius="25dp" app:cardElevation="10dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/txtInputPhone" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="20dp" android:layout_marginRight="5dp" android:hint="@string/hint_login_page" app:boxStrokeColor="#4E4A4A" app:errorEnabled="true" app:shapeAppearanceOverlay="@style/Button4"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/edtPhone" android:layout_width="match_parent" android:layout_height="wrap_content" android:digits="1234567890" android:fontFamily="@font/yekanbakh_regular" android:imeOptions="actionDone" android:inputType="number" android:textColor="@color/black" android:textSize="15sp" android:textStyle="normal" /> </com.google.android.material.textfield.TextInputLayout> <!-- 其他输入框和按钮代码保持原结构不变 --> </LinearLayout> </androidx.cardview.widget.CardView> </androidx.core.widget.NestedScrollView> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者jo jo
相关产品推荐
相关产品推荐

