SystemBarTint的使用(設置半透明狀態欄)

1.在系統是4.4以上的系統,包括4.4開始可以設置半透明的狀態欄了

代碼:

[html] view plain copy
  1. if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {  
  2.                                 //透明狀態欄  
  3.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  4.                                 //透明導航欄  
  5.                                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  6.                         }  

或者在style中設置主題:

[html] view plain copy
  1. <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">  
  2.   
  3.         <!-- API 19 theme customizations can go here. -->  
  4.         <item name="android:windowTranslucentStatus">true</item>  
  5.         <item name="android:windowTranslucentNavigation">true</item>  
  6.     </style>  

但是設置了這兩個屬性之後,佈局裏面的view會自動向上移動,顯示在透明狀態欄下面(就相當於狀態欄外層是framlayout),爲了防止這種現象,可以在主題中或者xml設置:

[html] view plain copy
  1. <item name="android:fitsSystemWindows">true</item>  
這樣佈局裏的view不會有任何移動,(就相當於狀態欄外層是linearlayout)。注意:在主題中設置該屬性會導致toast的顯示有異常,最好在佈局的最外層設置

設置瞭如上後:


由於使用了Theme.AppCompat.Light.DarkActionBar的主題,默認設置colorPrimaryDark的顏色:

[html] view plain copy
  1. <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>  
所以代碼設置的半透明效果被上面設置的顏色覆蓋了


如果設置了:

[html] view plain copy
  1. <item name="colorPrimaryDark">@android:color/transparent</item>  

設置了透明後就可以很好的顯示了


2.爲了兼容地版本,可以使用開源的框架SystemBarTint來實現(這個也只是兼容19以上的版本)

在api 19中是可以通過

(1).

[html] view plain copy
  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
這個設置狀態欄半透明,然後在佈局中設置:

[html] view plain copy
  1. android:fitsSystemWindows="true"  
這樣狀態欄就設置了半透明,且狀態欄與下面的view都是線性排列,這種情況下不能主動設置狀態欄的顏色,也不能通過佈局中的左邊menu以及中間主view的背景來改變狀態欄的顏色。



(2).

依舊設置:

[html] view plain copy
  1. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  

如果在actvity的主題中設置:

[html] view plain copy
  1. android:fitsSystemWindows="true"  
這樣也是狀態欄就設置了半透明,且狀態欄與下面的view都是線性排列,這種情況下不能主動設置狀態欄的顏色,只能通過設置佈局中左邊menu以及中間主view的背景來改變狀態欄的顏色,但是仔細看還是有一層半透明的顏色覆蓋在上面,因爲我們無法直接修改狀態欄的顏色。



(3).如果用SystemBarTint開源類,我們就可以主動改變狀態欄的顏色,但是隻能設置單色,不能像上面一樣隨着背景的改變而改變。

1.需要在主題中設置:

[html] view plain copy
  1. <style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">  
這個其實就是設置了:

[html] view plain copy
  1. <style name="Theme.Holo.Light.NoActionBar.TranslucentDecor">  
  2.         <item name="android:windowTranslucentStatus">true</item>  
  3.         <item name="android:windowTranslucentNavigation">true</item>  
  4.         <item name="android:windowContentOverlay">@null</item>  
  5.     </style>  
就相當於在代碼中設置,需要在在setContentView(layoutResID)之前調用
[html] view plain copy
  1. if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {  
  2.                                     //透明狀態欄  
  3.                                     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  4.                                     //透明導航欄  
  5.                                     getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  6.                             }  

2.然後在佈局的最外層或者activity的主題中設置:

[html] view plain copy
  1. android:fitsSystemWindows="true"  

3.然後在setContentView(layoutResID)之後調用代碼:

[html] view plain copy
  1. SystemBarTintManager tintManager = new SystemBarTintManager(this);  
  2.         tintManager.setStatusBarTintEnabled(true);  
  3.         tintManager.setStatusBarTintColor(Color.parseColor("#222231"));  
實現二度效果:


下面再使用如下主題的情況下:

[html] view plain copy
  1. Theme.AppCompat.Light.NoActionBar  
1.values

[html] view plain copy
  1. <style name="NoActionbarAppTheme" parent="Theme.AppCompat.Light.NoActionBar">  
  2.         <item name="colorPrimaryDark">@color/material_blue_700</item>  
  3.         <item name="colorPrimary">@color/material_blue_500</item>  
  4.         <item name="android:windowBackground">@color/white</item>  
  5.         <item name="android:textColorPrimary">@color/black</item>  
  6.         <item name="colorAccent">@color/material_green_A200</item>  
  7.     </style>  
不需要設置
[html] view plain copy
  1. android:fitsSystemWindows="true"  

2.values-v19

[html] view plain copy
  1. <style name="NoActionbarAppTheme_v19" parent="Theme.AppCompat.Light.NoActionBar">  
  2.         <item name="android:windowTranslucentNavigation">true</item>  
  3.         <item name="android:windowTranslucentStatus">true</item>  
  4.         <item name="colorPrimaryDark">@color/material_blue_700</item>  
  5.         <item name="colorPrimary">@color/material_blue_500</item>  
  6.         <item name="android:windowBackground">@color/white</item>  
  7.         <item name="android:textColorPrimary">@color/black</item>  
  8.         <item name="colorAccent">@color/material_green_A200</item>  
  9.     </style>  
佈局中需要設置
[html] view plain copy
  1. android:fitsSystemWindows="true"  

看效果與第一張類似,只是左邊菜單劃出的時候狀態欄上面有暗色的陰影

3.values-v21

[html] view plain copy
  1. <style name="NoActionbarAppTheme_v21" parent="Theme.AppCompat.Light.NoActionBar">  
  2.         <item name="colorPrimaryDark">@color/material_blue_700</item>  
  3.         <item name="colorPrimary">@color/material_blue_500</item>  
  4.         <item name="android:windowBackground">@color/bg</item>  
  5.         <item name="android:textColorPrimary">@color/black</item>  
  6.         <item name="colorAccent">@color/material_green_A200</item>  
  7.         <item name="colorControlHighlight">@color/material_blue_500</item>  
  8.         <item name="android:windowDrawsSystemBarBackgrounds">true</item>  
  9.         <item name="android:statusBarColor">@android:color/transparent</item>  
  10.     </style>  
佈局最外層需要設置
[html] view plain copy
  1. android:fitsSystemWindows="true"  
這個效果和上面的事一樣的(都是沉浸的效果)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章