網(wǎng)站首頁 編程語言 正文
前言
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
相關(guān)推薦
- 2022-07-06 C++實(shí)現(xiàn)中值濾波的示例代碼_C 語言
- 2022-10-14 Redis常見分布鎖的原理和實(shí)現(xiàn)_Redis
- 2022-04-09 Nginx 提示10013: An attempt was made to access a soc
- 2022-05-25 Python異常處理如何才能寫得優(yōu)雅(retrying模塊)_python
- 2022-08-28 centos 單機(jī)版redis安裝與數(shù)據(jù)持久化
- 2022-04-12 C語言打印楊輝三角形的示例代碼_C 語言
- 2022-05-22 Flutter利用Canvas繪制精美表盤效果詳解_Android
- 2022-12-23 C++類成員函數(shù)中的名字查找問題_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支