记录开源库 SystemBarTintManager 用法

曾使用过一次设置透明状态栏的开源库,地址:https://github.com/jgilfelt/SystemBarTint  部分手机未成功。在这记录搜寻到的另一份使用方式,目前手上的手机均通过。但是否完全可以用,还待有其他手机再测试。先记录代码如下:


  1. public void setStateBarColor(Activity activity) {  
  2.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  3.             //5.0 全透明实现  
  4.             //getWindow.setStatusBarColor(Color.TRANSPARENT)  
  5.             Window window = activity.getWindow();  
  6.             window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  7.             window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);  
  8.             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
  9.             window.setStatusBarColor(Color.TRANSPARENT);  
  10.         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  
  11.             //4.4 全透明状态栏  
  12.             activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  13.         }  
  14.         // 设置状态栏颜色  
  15.         ViewGroup contentLayout = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);  
  16.   
  17.         SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);  
  18.         SystemBarTintManager.SystemBarConfig config = systemBarTintManager.getConfig();  
  19.         int actionBarHeight = config.getActionBarHeight();  
  20.         contentLayout.getChildAt(0).setPadding(0, getStatusBarHeight(activity) + actionBarHeight, 00);  
  21.         if (mStatusBarColor == 0) {  
  22.             setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));  
  23.         } else {  
  24.             setupStatusBarView(activity, contentLayout, mStatusBarColor);  
  25.         }  
  26.         // 设置Activity layout的fitsSystemWindows  
  27.         View contentChild = contentLayout.getChildAt(0);  
  28.         contentChild.setFitsSystemWindows(true);//等同于在根布局设置android:fitsSystemWindows="true"  
  29.     }  
public void setStateBarColor(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT)
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        // 设置状态栏颜色
        ViewGroup contentLayout = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);

        SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
        SystemBarTintManager.SystemBarConfig config = systemBarTintManager.getConfig();
        int actionBarHeight = config.getActionBarHeight();
        contentLayout.getChildAt(0).setPadding(0, getStatusBarHeight(activity) + actionBarHeight, 0, 0);
        if (mStatusBarColor == 0) {
            setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));
        } else {
            setupStatusBarView(activity, contentLayout, mStatusBarColor);
        }
        // 设置Activity layout的fitsSystemWindows
        View contentChild = contentLayout.getChildAt(0);
        contentChild.setFitsSystemWindows(true);//等同于在根布局设置android:fitsSystemWindows="true"
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值