fitsSystemWindows与软键盘冲突
2022-10-27 本文已影响0人
馒头炖土豆
一、现象
页面根布局,因为沉浸式效果设置了fitsSystemWindows=true,当页面上有输入框拉起键盘的时候,导致页面展示异常
二、修改
自定义根布局,重载onApplyWindowInsets,替换SystemWindowInsets
class FitConstraintLayout(context: Context, attributeSet: AttributeSet) : ConstraintLayout(context, attributeSet) {
/**
* 修复 fitsSystemWindows 与软键盘的冲突问题
* */
override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets {
val copyInsets = insets?.let {
val origin = WindowInsetsCompat.toWindowInsetsCompat(it)
origin.replaceSystemWindowInsets(0, origin.systemWindowInsetTop, 0, 0)
}
return super.onApplyWindowInsets(copyInsets?.toWindowInsets()?:insets)
}
}