WindowInsetsController是android官方在api30之后提供,用于控制window的控制類,實作window控制元件的簡單化
要使用WindowInsetsController,需要先將core版本提高到1.5.0以上:
implementation 'androidx.core:core:1.5.0-alpha05'
實體化WindowInsetsController:
val controller = ViewCompat.getWindowInsetsController(view)
控制狀態欄
顯示狀態欄:
controller?.show(WindowInsetsCompat.Type.statusBars())
隱藏狀態欄:
controller?.hide(WindowInsetsCompat.Type.statusBars())
狀態欄文字顏色改為黑色:
controller?.isAppearanceLightStatusBars = true
狀態欄文字顏色改為白色:
controller?.isAppearanceLightStatusBars = false
控制導航欄
顯示
controller?.show(WindowInsetsCompat.Type.navigationBars())
隱藏
controller?.hide(WindowInsetsCompat.Type.navigationBars())
//導航欄隱藏時手勢操作
controller?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
//systemBarsBehavior有三個值:
BEHAVIOR_SHOW_BARS_BY_SWIPE
BEHAVIOR_SHOW_BARS_BY_TOUCH
BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
目前可能跟部分手機手勢有沖突
控制元件鍵盤
(需要界面有EditText并且EditText獲取到焦點才能起作用)
顯示鍵盤
controller?.show(WindowInsetsCompat.Type.ime())
隱藏鍵盤
controller?.hide(WindowInsetsCompat.Type.ime())
操作所有系統欄
全屏顯示:
controller?.hide(WindowInsetsCompat.Type.systemBars())
顯示其他系統欄:
controller?.show(WindowInsetsCompat.Type.systemBars())
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/238583.html
標籤:其他
上一篇:Android Studio 2020新版本 卡在Gradle downloading / sync failed / 下載緩慢 / 下載超時 親測有效解決辦法
