Android配置adjustPan后多行EditText被RecyclerView遮挡不生效问题
解决方案
问题根因
adjustPan的工作机制是整体平移窗口避让软键盘,不会重新测量布局尺寸,遇到多行EditText时,系统默认认为当前输入框已展示的区域足够输入,不会触发足够距离的平移。同时你当前布局使用百分比Guideline固定了RecyclerView和输入框的分界位置,adjustPan不会修改Guideline的百分比占比,会导致RecyclerView下压遮挡输入框。
修复步骤
- 第一步:修改AndroidManifest中Activity的windowSoftInputMode配置,替换为
adjustResize,该模式会重新计算布局可用高度,自动挤压上层控件为软键盘让出空间:
<activity android:name="XYZActivity" android:windowSoftInputMode="adjustResize"/>
- 第二步:优化布局约束,移除冗余的百分比Guideline,直接将RecyclerView底部与输入容器顶部绑定,同时修改EditText高度配置适配多行输入:
修改后的Chat.xml核心代码如下:
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:fitsSystemWindows="true"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:paddingStart="10dp" android:paddingEnd="10dp" android:paddingBottom="10dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" app:stackFromEnd="true" app:layout_constraintBottom_toTopOf="@+id/inputContainer" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/inputContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/grey" android:paddingTop="10dp" android:paddingBottom="10dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <EditText android:id="@+id/editTextContainer" android:layout_width="200dp" android:layout_height="wrap_content" android:maxLines="4" android:fadeScrollbars="false" android:inputType="textMultiLine" android:scrollbars="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
主要修改点:
- 根布局添加
android:fitsSystemWindows="true",避免布局内容偏移到状态栏下方 - 移除了百分比Guideline控件
- RecyclerView添加
app:stackFromEnd="true",聊天内容默认从底部开始展示,软键盘弹出时自动上滚列表 - EditText高度改为
wrap_content,新增android:maxLines="4"限制输入框最大高度,超过后内容滚动 - RecyclerView底部约束直接绑定到inputContainer顶部,inputContainer仅保留底部约束到父布局底部的规则
- 第三步:如果你的Activity主题设置了透明状态栏/导航栏,需要额外给主题添加
<item name="android:windowDrawsSystemBarBackgrounds">true</item>保证adjustResize生效。
内容的提问来源于stack exchange,提问作者Compose Learner
相关产品推荐
相关产品推荐

