ViewGroup inside CollapsingToolbarLayout show extra bottom padding when set fitsSystemWindows to be true

前端 未结 7 2342
有刺的猬
有刺的猬 2021-02-08 15:05

I am building an application and there is a profile fragment which shows a profile background image and some other elements. Like the screenshot below:

My quest

相关标签:
7条回答
  • 2021-02-08 16:08

    this problem maybe caused by this commit

    I fixed it programmatically with the way comment by @Bogdan Zurac.

    mCollapseContentLy = findViewById(R.id.ly_collapse_content);
    mCollapseContentLy.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int measuredHeight = mCollapseContentLy.getMeasuredHeight();
            ViewGroup.LayoutParams lp = mCollapsingToolbarLayout.getLayoutParams();
            layoutParams.height = measuredHeight;
            mCollapsingToolbarLayout.setLayoutParams(lp);
            mCollapseContentLy.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
    

    Edit after accepted

    create a new class extend CollapsingToolbarLayout and override the onMeasure method

    package android.support.design.widget;
    
    import android.content.Context;
    import android.util.AttributeSet;
    
    public class GodCollapsingToolbarLayout extends CollapsingToolbarLayout {
    
        public GodCollapsingToolbarLayout(Context context) {
            this(context, null);
        }
    
        public GodCollapsingToolbarLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public GodCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            final int mode = MeasureSpec.getMode(heightMeasureSpec);
            final int topInset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;
            if (mode == MeasureSpec.UNSPECIFIED && topInset > 0) {
                // fix the bottom empty padding
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        getMeasuredHeight() - topInset, MeasureSpec.EXACTLY);
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题