WindowManager设置添加的View全屏(覆盖状态栏和导航栏)

发布时间:2024年01月04日

关键是修改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?

文章来源:https://blog.csdn.net/LINDAMAN00/article/details/135385685
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。