本來研究了1天的狀態(tài)欄钉迷,準備截圖錄屏上手寫筆記了出皇,然后看到了這個
Android 系統(tǒng)狀態(tài)欄沉浸式/透明化完整解決方案
绸硕,然后我就把截圖啊啥的delete~~~去了堂竟,大綱都刪了QAQ
5月17日意外發(fā)現(xiàn)
Android開發(fā):Translucent System Bar 的最佳實踐
不過還是整理下吧,作為補充
實現(xiàn)狀態(tài)欄透明化
①. 使用主題玻佩,只要是帶Translucent的(當然這是活動是繼承自Activity而不是AppCompatActivity)
<style name="Scr" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor" />
② 普通的主題下加上
<item name="android:windowTranslucentStatus">true</item>
注意3鲟凇!咬崔!布局xml里root節(jié)點下加了
android:fitsSystemWindows="true"
作用大致就是擴張顯示到全屏税稼,上面那篇博文的中間往后位置有圖有介紹
由
變成了這樣
其實感覺變丑了
類似的這里頭還可以設置把導航欄的虛擬按鍵也設置成透明的
<item name="android:windowTranslucentNavigation">true</item>
像這樣
③有時候會發(fā)現(xiàn)在主題了設置的屬性沒有起作用,那就只能在code中擼一遍了
只要在Activity加載布局之后添加
Window window = getWindow();
//狀態(tài)欄透明
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//類似的垮斯,這個是導航欄透明
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//這里還有很多FlAG郎仆,更多請查API文檔
......
當然有個高級的使用方式,這么寫
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
當前的屬性標簽和自己想設置的一個都不會漏
狀態(tài)的顏色設定
來看張圖吧
里面這些對應的是Activity主題的item
比如
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
修改顏色只需要更改color值
例外有個開源庫SystemBarTint
它可以設定和改變狀態(tài)欄的背景色兜蠕,不過在版本19以上完全可以用透明模式背景來給狀態(tài)欄啊導航欄染色了扰肌。