日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Android最新狀態(tài)欄處理介紹_Android

作者:朱?江 ? 更新時間: 2022-02-23 編程語言

前言

Android 中狀態(tài)欄的處理無非兩種,一種是顯示隱藏狀態(tài)欄,另外一種是狀態(tài)欄字體顏色的修改,之前的寫法都已經(jīng)廢棄了,來看看最新的版本中應(yīng)該如何處理吧。

顯示隱藏狀態(tài)欄

先來看下之前的寫法吧:

/**
 * 設(shè)置透明狀態(tài)欄
 */
fun Activity.transparentStatusBars() {
    val option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    val vis = window.decorView.systemUiVisibility
    window.decorView.systemUiVisibility = option or vis
    window.statusBarColor = Color.TRANSPARENT
}

這樣看著是沒有什么問題,但是。。。來看下代碼的截圖吧:

在這里插入圖片描述

發(fā)現(xiàn)了沒有,咱們一直使用的方法其實(shí)都廢棄了。。。點(diǎn)擊去看下描述:

@deprecated SystemUiVisibility flags are deprecated. Use {@link WindowInsetsController}

可以看到官方讓使用 WindowInsetsController 來替換之前的寫法,其實(shí) WindowInsetsController 是一個接口,可以通過 ViewCompat.getWindowInsetsController 來進(jìn)行實(shí)例化,來看下如何使用吧:

/**
 * 設(shè)置透明狀態(tài)欄
 */
fun Activity.transparentStatusBar() {
    val controller = ViewCompat.getWindowInsetsController(window.decorView)
    // 隱藏狀態(tài)欄
    controller?.hide(statusBars())
    // 設(shè)置狀態(tài)欄顏色為透明
    window.statusBarColor = Color.TRANSPARENT
}

狀態(tài)欄字體顏色修改

同上面一樣,先來看下之前的代碼:

/**
 * 狀態(tài)欄反色
 */
fun Activity.setAndroidNativeLightStatusBars() {
    val decor = window.decorView
    if (!isDarkMode()) {
        decor.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    } else {
        decor.systemUiVisibility =
            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    }
}

同樣看著沒有問題,來看下代碼的截圖吧:

在這里插入圖片描述

和上面設(shè)置顯示隱藏狀態(tài)欄一樣,同樣是使用 WindowInsetsController 來替換之前的寫法:

/**
 * 狀態(tài)欄反色
 */
fun Activity.setAndroidNativeLightStatusBar() {
    val controller = ViewCompat.getWindowInsetsController(window.decorView)
    controller?.isAppearanceLightStatusBars = !isDarkMode()
}

上面中的 isDarkMode 是我寫的一個擴(kuò)展方法,用來判斷當(dāng)前是否為深色模式,來看下如何實(shí)現(xiàn)的吧:

/**
 * 獲取當(dāng)前是否為深色模式
 * 深色模式的值為:0x21
 * 淺色模式的值為:0x11
 * @return true 為是深色模式   false為不是深色模式
 */
fun Context.isDarkMode(): Boolean {
    return resources.configuration.uiMode == 0x21
}

輸入法顯示與否

其實(shí)官方現(xiàn)在都讓咱們使用 WindowInsetsController 來處理狀態(tài)欄或者導(dǎo)航欄,甚至能處理輸入法的顯示與否,只需要更換 hide 和 show 的類型即可:

/**
 * 隱藏ime
 */
fun Activity.hideIme() {
    val controller = ViewCompat.getWindowInsetsController(window.decorView)
    controller?.hide(ime())
}

/**
 * 顯示ime
 */
fun Activity.showIme() {
    val controller = ViewCompat.getWindowInsetsController(window.decorView)
    controller?.show(ime())
}

總結(jié)

說了這么多還沒放 Github 地址呢:https://github.com/zhujiang521/PlayWeather

原文鏈接:https://blog.csdn.net/haojiagou/article/details/121952817

欄目分類
最近更新