沉浸式狀態(tài)欄和透明狀態(tài)欄學(xué)習(xí)

關(guān)于沉浸式狀態(tài)欄和透明化系統(tǒng)欄的官方說明

  • Immersive full-screen mode

To provide your app with a layout that fills the entire(全部的) screen宛蚓,the new SYSTEM_UI_FLAG_IMMERSIVE flag for setSystemUIVisivility()(when combined(結(jié)合) with SYSTEM_UI_FLAG_HIDE_NAVIGATION enables a new immersivefull-screen mode.while immersive full-screen mode is enable, your activity continues to receive all touch events, The user can reveal(展現(xiàn)译红,披露) the system bars with an inward(向內(nèi)的) swipe(揮擊) along the region where the system bars normally appear. This clears the SYSTEM_UI_FIAG_HIDE_NAVIGATION flag (and the SYSTEM_UI_FIAG_FULL_SCREEN flag, if applied) so the system bars remain visiable. However, if you'd like the system bars to hide again after a few moments, you can instead use the SYSTEM_UI_FIAG_IMMERSIVE_STICKY flag.

  • Translucent(透明化) system bars

You can now make the system bars partially translucent with the themes,
Theme.Holo.NoActionBar.TranslucentDecor and Theme.Holo.NoActionBar.TranslucentDecor. By enabling translucent system bars, your layout will fill the area behind the system bars, so you must also enable fitsSystemWindows for the portion(部分) of your layout that should not be covered by the system bars.
If you're creating a custom theme, set one of these themes as the parent theme or include the windowTranslucentNavigation and windowTranslucentStatus style properties in your theme.

關(guān)于透明狀態(tài)欄說明:透明狀態(tài)欄就是狀態(tài)欄的顏色和activity根布局顏色相同艘狭。

  • 和圖片顏色相同的透明狀態(tài)欄
  • 和根布局顏色相同的透明狀態(tài)欄

顏色方式

colorMethod

在 android studio 中庞钢,排列方式變成 project,然后生成 values-v19 文件夾和 values-v21 文件夾诲锹,在文件夾中新建 style.xml 文件菩暗,接著分別在不同的 style 中生成 theme


styles.jpg

values-v19/style.xml

<Style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavagation">true</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

values-v21/style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">#31c27c</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

將 activity 的 theme 設(shè)置為 AppTheme

這個生成 Toast 會產(chǎn)生位移,可以使用 Toast.makeText(getApplicationContext(), "toast", Toast.LENGTH_SHORT).show();

圖片方式

pictureMethod

values-v19/style.xml

<Style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavagation">true</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

values-v21/style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:fitsSystemWindows">true</item>
</style>

其實圖片方法和顏色方法殊途同歸术裸,可以直接使用相同的 <item name="android:statusBarColor">@android:color/transparent</item>來使用

除了在 values-v19 和 values-v21 的 style.xml 中使用 fitsSystemWindows 屬性倘是,還可以直接在根部局中加上 android:fitsSystemWindows="true" 屬性,這個方法就沒有 toast 位移的問題袭艺,但是需要在每一個activity的根布局中加入這個屬性搀崭,比較麻煩,我們還可以使用以下方法來解決重復(fù)書寫 fitsSystemWindows:

  • 新建一個TranslucentBarBaseActivity

      public abstract class TranslucentBarBaseActivity extends AppCompatActivity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    
      setContentView(getLayoutResId());//把設(shè)置布局文件的操作交給繼承的子類
    
      ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
      View parentView = contentFrameLayout.getChildAt(0);
      if (parentView != null && Build.VERSION.SDK_INT >= 14) {
          parentView.setFitsSystemWindows(true);
      }
      }
    
          /**
          * 返回當(dāng)前Activity布局文件的id
          *
          *@return
          */
          abstract protected int getLayoutResId();
      }
    

    然后主 activiy 繼承此 activiy猾编,就可以了瘤睹。

以上文檔引用自 Android開發(fā):Translucent System Bar 的最佳實踐

TranslucentBarDemo 中可以看到代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市答倡,隨后出現(xiàn)的幾起案子轰传,更是在濱河造成了極大的恐慌,老刑警劉巖瘪撇,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件获茬,死亡現(xiàn)場離奇詭異,居然都是意外死亡设江,警方通過查閱死者的電腦和手機锦茁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叉存,“玉大人码俩,你說我怎么就攤上這事〖吣螅” “怎么了稿存?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵笨篷,是天一觀的道長。 經(jīng)常有香客問我瓣履,道長率翅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任袖迎,我火速辦了婚禮冕臭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘燕锥。我一直安慰自己辜贵,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布归形。 她就那樣靜靜地躺著托慨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪暇榴。 梳的紋絲不亂的頭發(fā)上厚棵,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音蔼紧,去河邊找鬼婆硬。 笑死,一個胖子當(dāng)著我的面吹牛歉井,可吹牛的內(nèi)容都是我干的柿祈。 我是一名探鬼主播哈误,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼哩至,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蜜自?” 一聲冷哼從身側(cè)響起菩貌,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎重荠,沒想到半個月后箭阶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡戈鲁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年仇参,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婆殿。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡诈乒,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出婆芦,到底是詐尸還是另有隱情怕磨,我是刑警寧澤喂饥,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站肠鲫,受9級特大地震影響员帮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜导饲,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一捞高、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧渣锦,春花似錦棠枉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至娄猫,卻和暖如春贱除,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背媳溺。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工月幌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悬蔽。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓扯躺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蝎困。 傳聞我的和親對象是個殘疾皇子录语,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 關(guān)于沉浸式狀態(tài)欄一詞的說法從何而來我們無從考證。但這確實是個錯誤的說法先引用官方的一段話禾乘。 Immersive f...
    ifjgm閱讀 9,075評論 3 46
  • 概述 在一個人云亦云的時代澎埠,在技術(shù)迅猛發(fā)展的時代,我們來不及去好好學(xué)習(xí)始藕,好好理解蒲稳,就這樣和著別人說話,都沒有好好思...
    Lshare_Blog閱讀 15,026評論 13 41
  • 前言 首先請大家看幾張圖: 以上的效果伍派,一般我們統(tǒng)稱為沉浸式狀態(tài)欄江耀。其實,這種叫法不是很準(zhǔn)確诉植,而且也沒有沉浸式狀態(tài)...
    宇是我閱讀 3,843評論 2 28
  • 一提到沉浸式狀態(tài)欄祥国,第一個浮現(xiàn)在腦海里的詞就是“碎片化”。碎片化是讓 Android 開發(fā)者很頭疼的問題倍踪,相信沒有...
    揚州慢_閱讀 176,148評論 30 300
  • 生命中的痛系宫,大多都無法言說索昂。 有時候并不是不能說,而是不愿說扩借。 不愿自己變成一只搖尾乞憐的狗椒惨。 從未如此強烈感受到...
    茶小星閱讀 3,550評論 0 1