关键是修改WindowManager.LayoutParams
关键参数是设置忽略父窗口中的任何约束且允许窗口扩展到屏幕之外
params.flags =?WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
和设置手机真正的高度(包括导航栏和状态栏)
val screenSize = Point()
windowManager.defaultDisplay.getRealSize(screenSize)
params.height = screenSize.y
完整代码:?
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val params = WindowManager.LayoutParams()
// 设置Window Type
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
else params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// 设置全屏最重要的参数是FLAG_LAYOUT_IN_SCREEN、FLAG_LAYOUT_NO_LIMITS
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
WindowManager.LayoutParams.FLAG_FULLSCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
params.gravity = Gravity.START or Gravity.TOP
params.format = PixelFormat.RGBA_8888
val screenSize = Point()
windowManager.defaultDisplay.getRealSize(screenSize)
params.width = screenSize.x
params.height = screenSize.y //设置手机真正的分辨率高度(包括导航栏和状态栏)
// 添加你需要显示的布局
windowManager.addView(view, params)
参考大佬的文章:https://gist.github.com/kakajika/973667ccc945a1ad10e6eba0cef0f793?