沉浸式狀態(tài)欄的探討

沉浸式狀態(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)

參考

Android9.0 完全隱藏導(dǎo)航欄悬襟、狀態(tài)欄

Android沉浸式狀態(tài)欄實現(xiàn)

Android 5.0 如何實現(xiàn)將布局的內(nèi)容延伸到狀態(tài)欄?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末衅码,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子脊岳,更是在濱河造成了極大的恐慌,老刑警劉巖垛玻,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件割捅,死亡現(xiàn)場離奇詭異,居然都是意外死亡帚桩,警方通過查閱死者的電腦和手機亿驾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來账嚎,“玉大人莫瞬,你說我怎么就攤上這事」叮” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵召锈,是天一觀的道長旁振。 經(jīng)常有香客問我,道長涨岁,這世上最難降的妖魔是什么拐袜? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮梢薪,結(jié)果婚禮上蹬铺,老公的妹妹穿的比我還像新娘。我一直安慰自己秉撇,他們只是感情好甜攀,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著畜疾,像睡著了一般赴邻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上啡捶,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天姥敛,我揣著相機與錄音,去河邊找鬼瞎暑。 笑死彤敛,一個胖子當著我的面吹牛与帆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播墨榄,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼玄糟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了袄秩?” 一聲冷哼從身側(cè)響起阵翎,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎之剧,沒想到半個月后郭卫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡背稼,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年贰军,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蟹肘。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡词疼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出帘腹,到底是詐尸還是另有隱情贰盗,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布竹椒,位于F島的核電站童太,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏胸完。R本人自食惡果不足惜书释,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赊窥。 院中可真熱鬧爆惧,春花似錦、人聲如沸锨能。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽址遇。三九已至熄阻,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間倔约,已是汗流浹背秃殉。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人钾军。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓鳄袍,卻偏偏與公主長得像,于是被迫代替她去往敵國和親吏恭。 傳聞我的和親對象是個殘疾皇子拗小,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361