Android 全屏显示(兼容API30)

作者: 枫未晚 | 来源:发表于2022-03-30 09:49 被阅读0次

有时候app需要隐藏掉状态栏,让app全屏显示,在API 30以前写法都是使用的下方这种,网上也大多是这种:

window.setFlags(
    WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN
)

但在API 30以后,这个写法用 WindowInsetsController 接口的 hide 方法替代了,具体如下:

window.insetsController?.hide(WindowInsets.Type.statusBars())

在上方使用的是kotlin写法,如果是java可以用下方写法:

getWindow().getInsetsController().hide(WindowInsets.Type.statusBars());

兼容API 30以前的写法:

with(window) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
        insetsController?.hide(WindowInsets.Type.statusBars())
    else
        setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
}

相关文章

网友评论

    本文标题:Android 全屏显示(兼容API30)

    本文链接:https://www.haomeiwen.com/subject/imwvjrtx.html