BottomSheet DialogFragment被键盘遮挡问题求助
解决BottomSheetDialogFragment中键盘遮挡按钮的问题
我完全懂你这种头疼的情况——点击BottomSheet里的EditText弹出软键盘后,底部的按钮直接被挡住,试了之前的方案还没解决,太闹心了!下面给你几个亲测有效的解决办法,一步步来:
方案一:调整Dialog窗口的软键盘适配模式
默认的BottomSheetDialog可能没有正确设置软键盘的调整策略,我们可以在onCreateDialog方法里手动配置,让窗口在键盘弹出时自动调整大小:
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.setOnShowListener(dialogInterface -> { BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface; Window window = bottomSheetDialog.getWindow(); if (window != null) { // 让窗口随键盘弹出自动调整大小,BottomSheet会被顶起 window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); } }); return dialog; }
这个设置会让系统在软键盘弹出时重新计算窗口高度,BottomSheet会自动向上移动,按钮自然就不会被挡住了。
方案二:让BottomSheet自适应内容高度
给BottomSheetBehavior设置fitToContents=true,让它根据内容自动调整高度,避免固定peekHeight导致的遮挡问题:
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ConstraintLayout bottomSheet = view.findViewById(R.id.bottomSheet); BottomSheetBehavior<ConstraintLayout> behavior = BottomSheetBehavior.from(bottomSheet); // 开启内容自适应,BottomSheet会根据内部内容高度调整自身高度 behavior.setFitToContents(true); // 可选:如果希望默认展开到内容高度,而不是固定的peekHeight behavior.setState(BottomSheetBehavior.STATE_EXPANDED); }
方案三:给内容添加滚动兜底
如果前面的方案还是有极端情况(比如某些机型适配问题),可以把EditText和Button放在NestedScrollView里,这样即使被键盘挡住,用户也能滚动看到按钮:
修改你的布局文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/bottomSheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/contacts_bottom_sheet_background" app:behavior_hideable="true" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" app:behavior_peekHeight="500dp"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/insert_user" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <Button android:id="@+id/add_user_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/contacts_add" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/insert_user"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
额外注意事项
检查你对应的Activity在AndroidManifest.xml中的windowSoftInputMode设置,不要和我们的配置冲突,比如避免设置SOFT_INPUT_ADJUST_PAN,最好设为SOFT_INPUT_ADJUST_RESIZE或者不设置(默认值即可):
<activity android:name=".YourActivity" android:windowSoftInputMode="adjustResize"> </activity>
优先组合方案一+方案三,基本能覆盖绝大多数场景啦!
内容的提问来源于stack exchange,提问作者Jose Gonzalez
相关产品推荐
相关产品推荐

