Google發布了Android新的大版本M,也進一步加強了應用沉浸式的支持,問題是,沉浸之后,如果界面也是淺色的,就會造成圖標看不清楚的問題。因此,M之后,Android原生就支持狀態欄圖標的黑白色處理了。
M上面增加了一個Flag(以下內容引自Google SDK文檔):
public static final int SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
Added in API level 23
Flag for setSystemUiVisibility(int)
: Requests the status bar to draw in a mode that is compatible with light status bar backgrounds.
For this to take effect, the window must request FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
but not FLAG_TRANSLUCENT_STATUS
.
See Also
windowLightStatusBar
Constant Value: 8192 (0x00002000)
只要使用這個Flag,就可以設置狀態欄圖標為黑色或者白色(其實按照本意,是告訴系統狀態欄頂部是白色的,需要按一個合適的模式去繪制狀態欄,當然,其實就是黑色)。
具體使用方法:
public void setDarkStatusIcon(boolean bDark) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ View decorView = getWindow().getDecorView(); if(decorView != null){ int vis = decorView.getSystemUiVisibility(); if(bDark){ vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } else{ vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; } decorView.setSystemUiVisibility(vis); } } }
這個Flag只有在使用了FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
並且沒有使用 FLAG_TRANSLUCENT_STATUS的時候才有效,也就是只有在狀態欄全透明的時候才有效。