Android 4.4 — Translucent status/navigation bars — fitsSystemWindows/clipToPadding don't work through fragment transactions

前端 未结 8 2095
余生分开走
余生分开走 2021-01-30 11:30

When using the translucent status and navigation bars from the new Android 4.4 KitKat APIs, setting fitsSystemWindows=\"true\" and clipToPadding=\"false\"

8条回答
  •  生来不讨喜
    2021-01-30 11:56

    Combined with @BladeCoder answer i've created FittedFrameLayout class which does two things:

    • it doesn't add padding for itself
    • it scan through all views inside its container and add padding for them, but stops on the lowest layer (if fitssystemwindows flag is found it won't scan child deeper, but still on same depth or below).

      public class FittedFrameLayout extends FrameLayout {
          private Rect insets = new Rect();
      
          public FittedFrameLayout(Context context) {
              super(context);
          }
      
          public FittedFrameLayout(Context context, AttributeSet attrs) {
              super(context, attrs);
          }
      
          public FittedFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
              super(context, attrs, defStyleAttr);
          }
      
          @TargetApi(Build.VERSION_CODES.LOLLIPOP)
          public FittedFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
              super(context, attrs, defStyleAttr, defStyleRes);
          }
      
          protected void setChildPadding(View view, Rect insets){
              if(!(view instanceof ViewGroup))
                  return;
      
              ViewGroup parent = (ViewGroup) view;
              if (parent instanceof FittedFrameLayout)
                  ((FittedFrameLayout)parent).fitSystemWindows(insets);
              else{
                  if( ViewCompat.getFitsSystemWindows(parent))
                      parent.setPadding(insets.left,insets.top,insets.right,insets.bottom);
                  else{
                      for (int i = 0, z = parent.getChildCount(); i < z; i++)
                          setChildPadding(parent.getChildAt(i), insets);
                  }
              }
          }
      
          @Override
          protected boolean fitSystemWindows(Rect insets) {
              this.insets = insets;
              for (int i = 0, z = getChildCount(); i < z; i++)
                  setChildPadding(getChildAt(i), insets);
      
              return true;
          }
      
          @Override
          public void addView(View child, int index, ViewGroup.LayoutParams params) {
              super.addView(child, index, params);
              setChildPadding(child, insets);
          }
      }
      

提交回复
热议问题