记录开源库 SystemBarTintManager 用法

来源:互联网 发布:黄河大侠知乎 编辑:程序博客网 时间:2024/04/27 14:39

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


[java] view plain copy print?
  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.     }  

原创粉丝点击