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"  
这个效果和上面的事一样的(都是沉浸的效果)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值