Android 44 沉浸式状态栏效果

这是我的第一遍文章

主要用来记录我在开发中遇到的问题或者是注意事项

这次是状态栏沉浸式效果

状态栏沉浸式效果在Android4.4以后才出现的
                            说下我所理解的沉浸式效果
                             所谓的沉浸式就是透明导航栏和状态栏
                                然后再把状态栏的颜色颜色修改为自己想要的颜色(一般是与标题栏),所以我们必须知道要把状态栏改为什么颜色
                                
             
              Android4.4就是sdk19
              所以我们做个函数
              @TargetApi(19)
private void initWindow(){
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);   
  }
  这个函数在onCreate()中调用
  这时我们会发现布局被拉伸了,状态栏没有了,这显然不是我们想要的结果
    这时我想在主布局中加个id
    然后使用
    layout_top.setPadding(0,getResources().getDimensionPixelSize(resourceId), 0, 0);将状态栏撑开
    最后就是给状态栏上色
    这边我们引入github以为大神的systembartint-1.0.4.jar 稍后提供jar包和github地址
    然后在我们的initWindow方法中添加
    SystemBarTintManager barTintManager=new SystemBarTintManager(this);
        barTintManager.setStatusBarTintEnabled(true);
        barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));
        即

   @TargetApi(19)
        private void initWindow(){
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
         layout_top.setPadding(0,getResources().getDimensionPixelSize(resourceId), 0, 0);
         SystemBarTintManager barTintManager=new SystemBarTintManager(this);
        barTintManager.setStatusBarTintEnabled(true);
        barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));  
  }
    
       这样就完成了我们的沉浸式状态栏
       
       是不是觉得一定一个ID然后再用findViewById(),有点麻烦?
      我从网上找到解决方法在布局文件root中添加
              android:fitsSystemWindows="true"
              这么一句话就可以省掉麻烦了
              
              
             @TargetApi(19)
        private void initWindow(){
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
         SystemBarTintManager barTintManager=new SystemBarTintManager(this);
        barTintManager.setStatusBarTintEnabled(true);
        barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));  
  }    
              
              
              
              
              
       下面是github的地址
       https://github.com/jgilfelt/SystemBarTint
       布吉岛博客怎么传jar包

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值