CoordinatorLayout status bar padding disappears from ViewPager 2nd page

后端 未结 4 1414
春和景丽
春和景丽 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: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;
    }
    

提交回复
热议问题