CoordinatorLayout not drawing behind status bar even with windowTranslucentStatus and fitsSystemWindows

前端 未结 4 976
旧时难觅i
旧时难觅i 2020-12-23 12:03

I am trying to draw views behind the status bar like this:

I tried to produce this effect with the recommended techniques, but I get this:

It\'s cl

4条回答
  •  抹茶落季
    2020-12-23 12:50

    edit for future readers: there's a lot of good information on the subject and the issue on the comments too, make sure to read through them.

    original answer: Your theme is wrong, that's why. Unfortunately, there're differences on how to activate in in Kitkat or Lollipop. On my code I did it in Java, but you can also achieve it in XML if you want to play with the V21 folders on your resources tree. The name of the parameters will be very similar to the Java counterparts.

    Delete the android:windowTranslucentStatus from your XML and in Java use like that:

       public static void setTranslucentStatusBar(Window window) {
          if (window == null) return;
          int sdkInt = Build.VERSION.SDK_INT;
          if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
             setTranslucentStatusBarLollipop(window);
          } else if (sdkInt >= Build.VERSION_CODES.KITKAT) {
             setTranslucentStatusBarKiKat(window);
          }
       }
    
      @TargetApi(Build.VERSION_CODES.LOLLIPOP)
       private static void setTranslucentStatusBarLollipop(Window window) {
          window.setStatusBarColor(
                 window.getContext()
                       .getResources()
                       .getColor(R.color. / add here your translucent color code /));
       }
    
       @TargetApi(Build.VERSION_CODES.KITKAT)
       private static void setTranslucentStatusBarKiKat(Window window) {
          window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
       }
    

    then you can call from your activity setTranslucentStatusBar(getWindow());

    edit:

    making the status bar translucent and drawing behind it (for some reason I cannot understand) are two separate tasks in Android.

    I've looked more on my code and I'm for sure have A LOT more android:fitsSystemWindows="true" on my layout than just the CoordinatorLayout.

    below are all the Views on my layout with android:fitsSystemWindows="true" on them:

    • CoordinatorLayout
    • AppBarLayout
    • CollapsingToolbarLayout
    • ImageView (with the background image)
    • FrameLayout (with the content of the header)

    so my suggestion is to just test/try filling up android:fitsSystemWindows="true" on your XML.

提交回复
热议问题