fitsSystemWindows effect gone for fragments added via FragmentTransaction

前端 未结 6 1044

I have an Activity with navigation drawer and full-bleed Fragment (with image in the top that must appear behind translucent system bar on Lollipop). While I had an interim

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 03:14

    My problem was similar to yours: I have a Bottom Bar Navigation which is replacing the content fragments. Now some of the fragments want to draw over the status bar (with CoordinatorLayout, AppBarLayout), others not (with ConstraintLayout, Toolbar).

    ConstraintLayout
      FrameLayout
        [the ViewGroup of your choice]
      BottomNavigationView
    

    The suggestion of ianhanniballake to add another CoordinatorLayout layer is not what I want, so I created a custom FrameLayout which handles the insets (like he suggested), and after some time I came upon this solution which really is not much code:

    activity_main.xml

    
    
        
    
        
    
    
    

    WindowInsetsFrameLayout.java

    /**
     * FrameLayout which takes care of applying the window insets to child views.
     */
    public class WindowInsetsFrameLayout extends FrameLayout {
    
        public WindowInsetsFrameLayout(Context context) {
            this(context, null);
        }
    
        public WindowInsetsFrameLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            // Look for replaced fragments and apply the insets again.
            setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
                @Override
                public void onChildViewAdded(View parent, View child) {
                    requestApplyInsets();
                }
    
                @Override
                public void onChildViewRemoved(View parent, View child) {
    
                }
            });
        }
    
    }
    

提交回复
热议问题