导航栏遮挡 PopupWindow 内容

Android 11 PopupWindow 内容区域会被 导航栏 遮挡,可以利用 FLAG_LAYOUT_IN_SCREEN 的 Flag 进行校正,如下:

private fun setLayoutInScreenEnabled(popup: PopupWindow) {
  // 修复安卓11导航栏遮挡问题
  if (Build.VERSION.SDK_INT > BUILD_VERSION_CODE_Q) {
    val wm = context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager
    var viewContent = popup.contentView
    while (viewContent != null && viewContent.layoutParams !is WindowManager.LayoutParams) {
      viewContent = viewContent.parent as? View
    }
    if (viewContent?.layoutParams is WindowManager.LayoutParams) {
      val params = viewContent.layoutParams as WindowManager.LayoutParams
      params.flags = params.flags or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
      wm?.updateViewLayout(viewContent, params)
    }
  }
}

你可能感兴趣的:(导航栏遮挡 PopupWindow 内容)