沉浸式狀態(tài)欄的探討
1. 常規(guī)theme
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- 下面這句是默認情況 -->
<!--<item name="android:windowTranslucentStatus">false</item>-->
</style>
default.png
2. windowTranslucentStatus true
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
可以看出狀態(tài)欄已經(jīng)是半透明了
windowTranslucentStatus.png
3. android:statusBarColor = @android:color/transparent
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!--<item name="android:windowTranslucentStatus">true</item>-->
<item name="android:windowTranslucentStatus">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
在2的基礎(chǔ)上設(shè)置狀態(tài)欄為全透明输莺,注意:這里圖片和網(wǎng)頁背景沖突忘苛,實際上狀態(tài)欄變成了全白色
statusBarTransparent.png
4. 3的解決方案踱启,windowTranslucentNavigation = true
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
可以看出狀態(tài)欄已經(jīng)變成了全透明色种远,不過也可以看見navigation bar變成了半透明色
excellent.png
5. 不過設(shè)置全透明的navigation bar
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
并沒有任何變化
transparentNavigation.png
6. 那嘗試設(shè)置其他顏色呢路操?
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:navigationBarColor">@android:color/holo_blue_bright</item>
</style>
并沒有效果,猜測魄梯,設(shè)置了透明操作欄之后沽讹,并不允許再設(shè)置操作欄的顏色
blueNavigation.png
7. 那回到原點,設(shè)置狀態(tài)欄顏色和操作欄顏色呢
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:statusBarColor">@android:color/holo_blue_dark</item>
<item name="android:navigationBarColor">@android:color/holo_blue_dark</item>
</style>
colorPrimaryDark
沒有效果了藻烤,直接設(shè)置狀態(tài)欄的顏色即可
blueAll.png
8. 全屏theme, 這個最簡單
<style name="FlagTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
但是還是有操作欄
fullscreen.png
9. 隱藏操作欄
使用theme
是無法實現(xiàn)這個效果的绷雏,只有通過代碼
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
根據(jù)google的說明,navigation非常重要怖亭,只是一個臨時的flag, 只要用戶有任何操作涎显,都會被清除。
/**
* Flag for {@link #setSystemUiVisibility(int)}: View has requested that the
* system navigation be temporarily hidden.
*
* <p>This is an even less obtrusive state than that called for by
* {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
* (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
* those to disappear. This is useful (in conjunction with the
* {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
* {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
* window flags) for displaying content using every last pixel on the display.
*
* <p>There is a limitation: because navigation controls are so important, the least user
* interaction will cause them to reappear immediately. When this happens, both
* this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
* so that both elements reappear at the same time.
*
* @see #setSystemUiVisibility(int)
*/
public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
There is a limitation: because navigation controls are so important, the least user
interaction will cause them to reappear immediately. When this happens, both
this flag and {@link #SYSTEM_UI_FLAG_FULLSCREEN} will be cleared automatically,
so that both elements reappear at the same time.
翻譯:
這存在一個限制兴猩,因為操作欄的控制非常重要期吓,所以用戶的任何操作都會讓它顯示出來。
在這種情況下倾芝,當操作欄顯示出來之后讨勤,這個flag和`SYSTEM_UI_FLAG_FULLSCREEN`將會被自動清除箭跳。
所以,所有的元素將同時出現(xiàn)