Android 沉浸式状态栏学习记录

Android使用沉浸式状态栏会让你的app看起来更加高大上,但是会产生很多兼容问题,一个app的activity肯定不止一个,有的时候,需要渐变的toolbar,有的时候,有需要DrawerLayout等布局,那沉浸式布局又会给你带来许多麻烦

下面是我认为沉浸式布局最好的定制方案了,因为app的每个activity都要实现沉浸式,所以我建议你可以写个BaseActivity让所有的activity继承他,这样我们可以在里面做很多activity共同的事,首先是xml文件的代码:



        

            

            

        

    

跟大家常见的布局并没有什么不同
就是在toolbar的外面套了一个FrameLayout布局,并添加了一个View(这个view就是状态栏的底色)
因为有时候,你的toolbar可能是白色的,你的状态栏上面的图标字体可能也是白色的,会看不见,你可设置#20000000半透明的灰色,比较接近google默认的底色
至于AppBarLayout 的 app:theme="@style/toolbar_dark_theme 则是自定义的them,在style文件中加入


    

    

 

    
    

接下来就是在activity的应用了:

   /**
    * 设置toolbar沉浸到任务栏
    * @param toolbar
    * 设置状态栏的底色
    * @param top_color
    */
   public void SetToolbar(Toolbar toolbar,int top_color){
       setSupportActionBar(toolbar);
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
           Window window = getWindow();
           window.clearFlags(
                   WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                           | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
           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(ContextCompat.getColor(this,top_color));
           window.setNavigationBarColor(Color.BLACK);
       } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           Window window = getWindow();
           window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                   WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
       }
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           if (Build.VERSION_CODES.LOLLIPOP>Build.VERSION.SDK_INT) {
               View paddingView = findViewById(R.id.bar_top);
               paddingView.setVisibility(View.VISIBLE);
               ViewGroup.LayoutParams params = paddingView.getLayoutParams();
               params.height = getStatusBarHeight();
           }
           toolbar.setFitsSystemWindows(true);
           toolbar.getLayoutParams().height = getAppBarHeight();
           toolbar.setPadding(toolbar.getPaddingLeft(),
                   getStatusBarHeight(),
                   toolbar.getPaddingRight(),
                   toolbar.getPaddingBottom());
       }
   }


private int getAppBarHeight() {
       return dip2px(56) + getStatusBarHeight();
   }

   private int getStatusBarHeight() {
       int result = 0;
       int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
       if (resourceId > 0) {
           result = getResources().getDimensionPixelSize(resourceId);
       }
       return result;
   }

   private int dip2px(float dipValue) {
       final float scale = getResources().getDisplayMetrics().density;
       return (int) (dipValue * scale + 0.5f);
   }

解释一下上面代码的意义
根据不同的系统版本,我们可以做的处理也是不一样的,Android 4.4 - Android 5.0 的Android 版本中并没有以下方法,所以没办法设置状态栏和低栏的底色

        window.setStatusBarColor(ContextCompat.getColor(this,top_color));
        window.setNavigationBarColor(ContextCompat.getColor(this,navigation_color));

但是我们可以自定义布局以及颜色:
1.先设置充满状态栏

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

2.自定义一个状态栏的布局,并设置他的底色:

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION_CODES.LOLLIPOP>Build.VERSION.SDK_INT) {
                View paddingView = findViewById(R.id.bar_top);
                paddingView.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams params = paddingView.getLayoutParams();
                params.height = getStatusBarHeight();
            }
            toolbar.setFitsSystemWindows(true);
            toolbar.getLayoutParams().height = getAppBarHeight();
            toolbar.setPadding(toolbar.getPaddingLeft(),
                    getStatusBarHeight(),
                    toolbar.getPaddingRight(),
                    toolbar.getPaddingBottom());
        }

3.补充一点,如果你发现状态栏的颜色依旧是原来的颜色,那就是说你在布局文件中设置了,删除就可以了

android:fitsSystemWindows="true"

至于5.0以上的沉浸式,Window类已经可以设置了,只要代码中加入以下代码就可以:

Window window = getWindow();
            window.clearFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                            | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            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(ContextCompat.getColor(this,top_color));
            window.setNavigationBarColor(Color.BLACK);

你可能感兴趣的:(Android 沉浸式状态栏学习记录)