Android Kotlin ConstraintLayout聊天页加底部输入框约束适配问题
正确布局配置
你更新1的思路是正确的,仅需删除inputContainer的多余顶部约束即可,完整正确布局代码如下:
<?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="#cccccc"> <FrameLayout android:id="@+id/conversationRecyclerViewViewWrapper" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/inputContainer" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/conversationRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:paddingStart="10dp" android:paddingEnd="10dp" android:paddingBottom="10dp" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" /> </FrameLayout> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/inputContainer" android:layout_width="0dp" android:layout_height="wrap_content" android:background="#ffffff" android:paddingStart="10dp" android:paddingTop="14dp" android:paddingEnd="10dp" android:paddingBottom="14dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent"> <EditText android:id="@+id/sendMessageEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="textMultiLine|text|textNoSuggestions" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
额外配置要求
要保证软键盘正常顶起布局,还需要在AndroidManifest.xml中给对应Activity添加配置:
<activity android:name=".你的聊天页Activity名" android:windowSoftInputMode="adjustResize" />
效果验证逻辑
- 单条消息场景:RecyclerView高度为内容高度,通过
layout_gravity="top"靠在FrameLayout顶部,符合顶部对齐的预期效果 - 多消息场景:RecyclerView内容高度超过外层FrameLayout的固定高度时,自动触发滚动逻辑,配合原本的
reverseLayout = true+stackFromEnd = false配置,最新消息默认展示在列表底部 - 软键盘唤起场景:
adjustResize配置会让Activity布局高度压缩到软键盘上方区域,输入框固定在布局底部,会自动跟随布局上移,同时列表区域也会同步压缩,不会被软键盘遮挡
内容的提问来源于stack exchange,提问作者Compose Learner
相关产品推荐
相关产品推荐

