1.在系統是4.4以上的系統,包括4.4開始可以設置半透明的狀態欄了
代碼:
- if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
- //透明狀態欄
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- //透明導航欄
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- }
或者在style中設置主題:
- <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
- <!-- API 19 theme customizations can go here. -->
- <item name="android:windowTranslucentStatus">true</item>
- <item name="android:windowTranslucentNavigation">true</item>
- </style>
但是設置了這兩個屬性之後,佈局裏面的view會自動向上移動,顯示在透明狀態欄下面(就相當於狀態欄外層是framlayout),爲了防止這種現象,可以在主題中或者xml設置:
- <item name="android:fitsSystemWindows">true</item>
設置瞭如上後:
由於使用了Theme.AppCompat.Light.DarkActionBar的主題,默認設置colorPrimaryDark的顏色:
- <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
如果設置了:
- <item name="colorPrimaryDark">@android:color/transparent</item>
設置了透明後就可以很好的顯示了
2.爲了兼容地版本,可以使用開源的框架SystemBarTint來實現(這個也只是兼容19以上的版本)
在api 19中是可以通過
(1).
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- android:fitsSystemWindows="true"
(2).
依舊設置:
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
如果在actvity的主題中設置:
- android:fitsSystemWindows="true"
(3).如果用SystemBarTint開源類,我們就可以主動改變狀態欄的顏色,但是隻能設置單色,不能像上面一樣隨着背景的改變而改變。
1.需要在主題中設置:
- <style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
- <style name="Theme.Holo.Light.NoActionBar.TranslucentDecor">
- <item name="android:windowTranslucentStatus">true</item>
- <item name="android:windowTranslucentNavigation">true</item>
- <item name="android:windowContentOverlay">@null</item>
- </style>
- if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
- //透明狀態欄
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- //透明導航欄
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
- }
2.然後在佈局的最外層或者activity的主題中設置:
- android:fitsSystemWindows="true"
3.然後在setContentView(layoutResID)之後調用代碼:
- SystemBarTintManager tintManager = new SystemBarTintManager(this);
- tintManager.setStatusBarTintEnabled(true);
- tintManager.setStatusBarTintColor(Color.parseColor("#222231"));
下面再使用如下主題的情況下:
- Theme.AppCompat.Light.NoActionBar
- <style name="NoActionbarAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="colorPrimaryDark">@color/material_blue_700</item>
- <item name="colorPrimary">@color/material_blue_500</item>
- <item name="android:windowBackground">@color/white</item>
- <item name="android:textColorPrimary">@color/black</item>
- <item name="colorAccent">@color/material_green_A200</item>
- </style>
- android:fitsSystemWindows="true"
2.values-v19
- <style name="NoActionbarAppTheme_v19" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="android:windowTranslucentNavigation">true</item>
- <item name="android:windowTranslucentStatus">true</item>
- <item name="colorPrimaryDark">@color/material_blue_700</item>
- <item name="colorPrimary">@color/material_blue_500</item>
- <item name="android:windowBackground">@color/white</item>
- <item name="android:textColorPrimary">@color/black</item>
- <item name="colorAccent">@color/material_green_A200</item>
- </style>
- android:fitsSystemWindows="true"
看效果與第一張類似,只是左邊菜單劃出的時候狀態欄上面有暗色的陰影
3.values-v21
- <style name="NoActionbarAppTheme_v21" parent="Theme.AppCompat.Light.NoActionBar">
- <item name="colorPrimaryDark">@color/material_blue_700</item>
- <item name="colorPrimary">@color/material_blue_500</item>
- <item name="android:windowBackground">@color/bg</item>
- <item name="android:textColorPrimary">@color/black</item>
- <item name="colorAccent">@color/material_green_A200</item>
- <item name="colorControlHighlight">@color/material_blue_500</item>
- <item name="android:windowDrawsSystemBarBackgrounds">true</item>
- <item name="android:statusBarColor">@android:color/transparent</item>
- </style>
- android:fitsSystemWindows="true"