WindowInsetsController 窗口控制器
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//android 30
val windowInsetsController = window.decorView.windowInsetsController
// 系統(tǒng)欄 前景深色
// windowInsetsController?.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
// 系統(tǒng)欄 前景淺色
windowInsetsController?.setSystemBarsAppearance(0, WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
// 默認(rèn)的行為,在 api33已過(guò)時(shí)了,推薦 BEHAVIOR_DEFAULT。 若是結(jié)合hide(),從隱藏欄的屏幕邊緣滑動(dòng)后,會(huì)固定顯示
windowInsetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE
// 如下 behavior 與 hide() 結(jié)合 后,從隱藏欄的屏幕邊緣滑動(dòng),系統(tǒng)欄會(huì)再次顯示且會(huì)在一段時(shí)間后再次自動(dòng)隱藏
// 若狀態(tài)欄和導(dǎo)航欄都設(shè)置了隱藏,那滑動(dòng)后,兩者會(huì)同時(shí)顯示
windowInsetsController?.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController?.hide(WindowInsets.Type.statusBars()) // 隱藏狀態(tài)欄
// 隱藏導(dǎo)航欄; 會(huì)將屏幕中的類似物理按鈕(back, home) 隱藏,需要滑一下才可見,使它們變成了虛擬按鍵
windowInsetsController?.hide(WindowInsets.Type.navigationBars())
}
有 hide() ,也有 show()
WindowInsetsControllerCompat 窗口控制器的兼容實(shí)現(xiàn)
implementation 'androidx.core:core-ktx:1.9.0'
在更早的版本中,使用ViewCompat.getWindowInsetsController()
獲取 WindowInsetsControllerCompat
實(shí)例
而現(xiàn)在推薦使用 WindowCompat.etInsetsController()
獲取 WindowInsetsControllerCompat
實(shí)例
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
windowInsetsController.isAppearanceLightNavigationBars = true // true, 則將導(dǎo)航欄的前景色更改為淺色 api >= 26
windowInsetsController.isAppearanceLightStatusBars = false // true, 則將狀態(tài)欄的前景色更改為淺色 api >= 23
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars()) // 隱藏狀態(tài)欄
windowInsetsController.hide(WindowInsetsCompat.Type.navigationBars()) // 隱藏導(dǎo)航欄
// 與 hide() 結(jié)合 后, 從隱藏欄的屏幕邊緣滑動(dòng),系統(tǒng)欄會(huì)再次顯示且會(huì)在一段時(shí)間后再次自動(dòng)隱藏
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
// 與 hide() 結(jié)合 后, 從隱藏欄的屏幕邊緣滑動(dòng)后,會(huì)固定顯示;isAppearanceLightStatusBars 設(shè)置為 false,狀態(tài)欄才是淺色
// windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
// 與 hide() 結(jié)合 后, 從隱藏欄的屏幕邊緣滑動(dòng)后,會(huì)固定顯示;isAppearanceLightStatusBars 設(shè)置為 false,狀態(tài)欄才是淺色
// windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH
設(shè)置背景色
window.statusBarColor = Color.BLUE // 狀態(tài)欄背景色
window.navigationBarColor = Color.BLUE() //應(yīng)用內(nèi) 導(dǎo)航欄,如 actionBar、底部虛擬按鍵背景
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.navigationBarDividerColor = Color.RED // 導(dǎo)航欄 分隔線的 顏色
}
什么是導(dǎo)航欄
底部的三個(gè)按鍵就是導(dǎo)航欄 (navigation bar): back / home / recent 。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-611793.html
高版本系統(tǒng),recent,可能沒有圖標(biāo)了,需要在那個(gè)位置"上滑"文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-611793.html
到了這里,關(guān)于Android WindowInsetsController 設(shè)置狀態(tài)欄、導(dǎo)航欄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!