CoordinatorLayout status bar padding disappears from ViewPager 2nd page

后端 未结 4 1404
春和景丽
春和景丽 2020-12-24 07:00

EDIT of 01/02/2016: Bug should be resolved by applying the code provided by Android Team: https://stackoverflow.com/a/35132144/3397345, see accepted answer

相关标签:
4条回答
  • 2020-12-24 07:40

    I ran into a similar issue (using v23.0.1). In my case, the problem occurs on the first page as well. The workaround I settled on was to adjust the padding and height of the toolbar when the fragment is created.

        // in onCreateView: adjust toolbar padding
        Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
        toolbar.getLayoutParams().height = toolbar.getLayoutParams().height + getStatusBarHeight();
    
        return mRootView;
    }
    
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-24 07:49

    Like sidecarcat, I ran into a similar issue (using v23.1.1). I post here a workaround by using the code of sidecarcat and add some code to remove superflous padding in some cases.

        // in onCreateView: adjust toolbar padding
        final int initialToolbarHeight = mToolbar.getLayoutParams().height;
        final int initialStatusBarHeight = getStatusBarHeight();
        mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int[] locToolbar = new int[2];
                mToolbar.getLocationOnScreen(locToolbar);
                int yToolbar = locToolbar[1];
                int topPaddingToolbar = mToolbar.getPaddingTop();
                if (isAdded()) {
                    //normal case : system status bar padding on toolbar : yToolbar = initialStatusBarHeight && topPaddingToolbar = 0
                    //abnormal case : no system status bar padding on toolbar -> toolbar behind status bar => add custom padding
                    if (yToolbar != initialStatusBarHeight && topPaddingToolbar == 0) {
                        mToolbar.setPadding(0, initialStatusBarHeight, 0, 0);
                        mToolbar.getLayoutParams().height = initialToolbarHeight + initialStatusBarHeight;
                    }
                    //abnormal case : system status bar padding and custom padding on toolbar -> toolbar with padding too large => remove custom padding
                    else if (yToolbar == initialStatusBarHeight && topPaddingToolbar == initialStatusBarHeight) {
                        mToolbar.setPadding(0, 0, 0, 0);
                        mToolbar.getLayoutParams().height = initialToolbarHeight;
                    }
                }
            }
        });
    
        return mRootView;
    }
    
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-12-24 08:02

    Solution proposed by Android Team in the answer of my reported defect. This code should finally work.

    ViewPager mViewPager;
    
    ViewCompat.setOnApplyWindowInsetsListener(mViewPager,
            new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v,
                WindowInsetsCompat insets) {
            insets = ViewCompat.onApplyWindowInsets(v, insets);
            if (insets.isConsumed()) {
                return insets;
            }
    
            boolean consumed = false;
            for (int i = 0, count = mViewPager.getChildCount(); i <  count; i++) {
                ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets);
                if (insets.isConsumed()) {
                    consumed = true;
                }
            }
            return consumed ? insets.consumeSystemWindowInsets() : insets;
        }
    });
    
    0 讨论(0)
  • 2020-12-24 08:05

    Seems the solution for this is simple

    You have

    android:fitsSystemWindows="true"
    

    In the fragment CoordinateLayoutView

    remove it from here and put in the Activity's root view.

    This should now all work

    0 讨论(0)
提交回复
热议问题