Android - Frame layout height not matching with coordinator layout

前端 未结 2 971
野性不改
野性不改 2021-02-14 07:12

I have a problem with my FrameLayout (Container in Drawer Layout). The height of the FrameLayout exceeds the screen height (below the android default menu buttons at bottom).

2条回答
  •  不知归路
    2021-02-14 07:53

    If you use different Fragments inside your CoordinatorLayout you will face the problem, that some Fragments have scrollable content and some should not scroll. Your Toolbar has the scrolling flags "scroll|enterAlways", which is ok for the former layouts, but not ok for the latter. My solution is a custom AppBarLayout.Behavior which switches the scrolling flags dependant on the custom tag (contentShouldNotScrollTag). Set this tag for the layouts, which should not scroll like this:

    
       
    
    

    As the result, the height of this Fragment will not exceed the screen's height. Here is the custom behavior class for the AppBarLayout:

    public class MyScrollBehavior extends AppBarLayout.Behavior {
        private View content;
    
        public MyScrollBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onMeasureChild(CoordinatorLayout parent, AppBarLayout appBarLayout, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
            if(content == null) {
                content = parent.findViewById(R.id.container);
            }
    
            if(content != null) {
                boolean shouldNotScroll = content.findViewWithTag(parent.getContext().getString(R.string.contentShouldNotScrollTag)) != null;
                Toolbar toolbar = (Toolbar) appBarLayout.findViewById(R.id.toolbar);
                AppBarLayout.LayoutParams params =
                        (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
                if (shouldNotScroll) {
                    params.setScrollFlags(0);
                    appBarLayout.setExpanded(true, true);
                } else {
                    params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                            | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                }
            }
    
            return super.onMeasureChild(parent, appBarLayout, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
        }
    }
    

提交回复
热议问题