CoordinatorLayout children are not fullscreen

前端 未结 3 735
醉酒成梦
醉酒成梦 2021-02-08 23:03

I have an Activity which is displayed fullscreen. This works perfectly with many layouts I have tried, except for when the CoordinatorLayout is the roo

3条回答
  •  抹茶落季
    2021-02-08 23:42

    I had a similar issue and had to hack a bit of a solution. Instead of an ImageView, my CoordinatorLayout child view is for streaming a video. Since I am streaming a video during rotation, I need to to watch for configChanges and override onConfigurationChanged. This may not work for you if you do not want to override onConfigurationChanged, but it may work out. Like I said, it's a bit of a hack, but works for me.

    I end up fully expanding the Toolbar, storing the offset, then hiding it (treating it as a standard view). Then when I rotate back, I am resizing it to whatever the offset was when the user rotated the device.

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            showToolbar();
            mViewPager.setVisibility(View.VISIBLE);
    
        } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            hideToolbar();
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mViewPager.setVisibility(View.GONE);
        }
    }
    
    public void hideToolbar()
    {
        setCurrentOffset();
        expandToolbarToHeight(0);
        findViewById(R.id.toolbar).setVisibility(View.GONE);
    }
    
    public void showToolbar()
    {
        expandToolbarToHeight(oldOffset);
        findViewById(R.id.toolbar).setVisibility(View.VISIBLE);
    }
    
    public void setCurrentOffset() {
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
        if (behavior != null) {
            oldOffset = behavior.getTopAndBottomOffset();
        }
    }
    
    public void expandToolbarToHeight(int height) {
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
        AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
        if (behavior != null) {
            behavior.setTopAndBottomOffset(height);
            behavior.onNestedPreScroll(mCoordinatorLayout, mAppBarLayout, null, 0, 1, new int[2]);
        }
    }
    

提交回复
热议问题