Android:软键盘弹出时按钮被遮挡 多种方案尝试无效求解决
我明白你遇到的这个头疼的问题——试过了各种windowSoftInputMode配置、换布局容器都没用,Android 6上就是顽固地把Button挡在键盘下面。结合你的布局结构和不能移除外层RelativeLayout的限制,咱们一步步来解决:
核心问题分析
在Android 6中,adjustResize的生效逻辑对布局层级和属性配置比较敏感,尤其是外层嵌套RelativeLayout、内部用ScrollView+ConstraintLayout的组合时,很容易因为视口填充、布局高度设置不合理,导致底部控件无法被键盘触发的布局resize推上来。
具体修复步骤
确保Manifest中配置正确的
windowSoftInputMode
优先在Activity对应的Manifest条目里配置,代码动态设置可能不如这个可靠:<activity android:name=".screens.news.leave.NewsDetailActivity" android:windowSoftInputMode="adjustResize" />给NestedScrollView添加
fillViewport属性
这个属性能让ScrollView的子布局填满整个视口,否则底部的Button可能因为ScrollView没有撑满高度,无法被键盘触发的resize动作带动上移:<androidx.core.widget.NestedScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbar" android:paddingTop="@dimen/margin_large" android:fillViewport="true"> <!-- 新增该属性 -->调整ConstraintLayout的高度属性
你当前给ConstraintLayout设置的android:layout_height="0dp"在ScrollView里不合理——ScrollView的子布局应该用wrap_content来适配内容高度,0dp会让ConstraintLayout依赖ScrollView的约束,导致底部控件的位置计算错误:<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 将0dp改为wrap_content -->给外层RelativeLayout添加
fitsSystemWindows属性
确保系统窗口(比如软键盘)的resize事件能正确传递到内部布局:<RelativeLayout 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:background="@color/white" android:fitsSystemWindows="true" <!-- 新增该属性 --> tools:context=".screens.news.leave.NewsDetailActivity">排查主题干扰
检查你的Activity主题,确保没有设置android:windowFullscreen="true"或android:windowTranslucentStatus="true"——这些属性会干扰adjustResize的生效逻辑,如果必须使用透明状态栏,可能需要额外处理,但优先去掉这些属性测试。
修改后的关键布局代码示例
<RelativeLayout 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:background="@color/white" android:fitsSystemWindows="true" tools:context=".screens.news.leave.NewsDetailActivity"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <androidx.core.widget.NestedScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/toolbar" android:paddingTop="@dimen/margin_large" android:fillViewport="true"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 其他视图内容... --> <EditText android:id="@+id/commentET" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_medium" android:layout_marginTop="@dimen/margin_medium" android:layout_marginEnd="@dimen/margin_medium" android:fontFamily="@font/roboto_regular" android:hint="Оставить комментарий" android:textColor="#878787" android:textSize="@dimen/textsize_large" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/commentsRV" /> <Button android:id="@+id/sendComment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_medium" android:layout_marginEnd="@dimen/margin_medium" android:layout_marginBottom="16dp" android:background="@drawable/button_selector" android:text="Оставить комментарий" android:textColor="@color/textWhite" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/commentET" /> <!-- 其他视图内容... --> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView> </RelativeLayout>
按照这些步骤调整后,Android 6上软键盘弹出时,Button应该会跟着EditText一起上移到键盘上方了。
内容的提问来源于stack exchange,提问作者Pavel Sarpov

