跳转浏览器支付后重新打开BottomSheetDialogFragment内容被挤出屏幕
BottomSheetDialogFragment跳转浏览器后重开高度异常解决方案
可行解决思路
- 修复WindowInsets异常消费
跳转浏览器返回后,Activity的状态栏、导航栏边距计算可能出现异常,导致BottomSheet误将全屏高度作为可用高度。可在弹窗onViewCreated中主动触发边距重计算:// 主动申请重新应用窗口边距 ViewCompat.requestApplyInsets(bottomSheetRoot) // 强制根布局适配系统窗口 bottomSheetRoot.fitsSystemWindows = true - 固定弹窗最大高度
现有布局根高度为wrap_content,异常场景下高度计算失效会导致拉伸溢出,可在弹窗onStart中手动限制最大高度:override fun onStart() { super.onStart() val bottomSheet = dialog?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) ?: return val behavior = BottomSheetBehavior.from(bottomSheet) // 取屏幕高度的80%作为弹窗最大高度,可自行调整比例 val maxHeight = (resources.displayMetrics.heightPixels * 0.8f).toInt() behavior.peekHeight = maxHeight bottomSheet.layoutParams.height = maxHeight behavior.state = BottomSheetBehavior.STATE_COLLAPSED } - 配置Activity避免不必要重建
跳转浏览器过程中如果触发横竖屏切换、UI模式变更等配置变化,未配置configChanges的Activity会自动重建,导致主题参数异常。可在AndroidManifest对应Activity节点添加配置:<activity android:name=".你的支付页Activity名称" android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout|uiMode" /> - 关闭弹窗时重置状态
每次弹窗关闭时主动清空Behavior残留的异常状态,避免影响后续弹窗打开:override fun onDismiss(dialog: DialogInterface) { super.onDismiss(dialog) val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) ?: return BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_HIDDEN }
异常效果截图

提问者提供的参考布局
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/bottomSheetRoot" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:behavior_hideable="true" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/handle" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" /> <com.telia.commonUI.views.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/handle"/> <androidx.core.widget.NestedScrollView android:id="@+id/nestedScroll" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintBottom_toBottomOf="parent"> <androidx.fragment.app.FragmentContainerView android:id="@+id/fragmentContainer" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="wrap_content" /> </androidx.core.widget.NestedScrollView> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者Algar
相关产品推荐
相关产品推荐

