效果:进入后状态栏、导航栏隐藏,手势滑动显示,2s后又自动隐藏
- style设置全屏(或者代码中设置)
<!--启用全屏-->
<item name="android:windowFullscreen">true</item>
- 在BaseActivity中设置沉浸模式
// 方法1,在onCreate中设置以下代码
hideSystemUiVisibility()
window.decorView.setOnSystemUiVisibilityChangeListener {
hideSystemUiVisibility()
}
// 方法2,重写 onWindowFocusChanged
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) hideSystemUiVisibility()
}
// 设置沉浸模式
private fun hideSystemUiVisibility() {
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
}
-
对特殊控件的处理(显示前取消焦点,显示后再加上焦点,避免FLAG改变)
- Dialog类的处理
重写show方法
override fun show() { window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) super.show() window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) }
- PopUpWindow类的处理
重写showAsDropDown方法
override fun showAsDropDown(anchor: View?, xoff: Int, yoff: Int, gravity: Int) { isFocusable = false super.showAsDropDown(anchor, xoff, yoff, gravity) contentView.systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION isFocusable = true update() }
- Spinner类的处理
通过Kotlin拓展方法,取消其焦点获取
fun Spinner.avoidDropdownFocus() { try { val listPopup = Spinner::class.java.getDeclaredField("mPopup").apply { isAccessible = true }.get(this) if (listPopup is ListPopupWindow) { val popup = ListPopupWindow::class.java.getDeclaredField("mPopup").apply { isAccessible = true }.get(listPopup) if (popup is PopupWindow) { popup.isFocusable = false } } } catch (e: Exception) { e.printStackTrace() } }
- 还有谁???
不隐藏状态栏的完美解决方法(5.0以后):
<item name="android:statusBarColor">@android:color/transparent</item>
配合
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
让在有抽屉的界面上面还是会有一层半透明遮罩
这时候需要在NavigationView
中设置app:insetForeground="#00000000"
当然,一般设备和一般界面设置windowTranslucentStatus
为true这一个属性就搞定了。
但是这种在锤子坚果R1上仍然有半透明遮罩,即使强行再设置statusBarColor
为自己想要的颜色,有抽屉界面即使按照上面方法设置了还是会有一层半透明遮罩,这个应该是系统默认的实现方式。
所以,还是别怕麻烦了。
网友评论