fitsSystemWindows与软键盘冲突

542 阅读1分钟

一、现象

页面根布局,因为沉浸式效果设置了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)
    }
}

三、结语

  1. 送人玫瑰 手留余香
  2. 如果对您有帮忙,请点赞支持我~~