DEV Community

Stone
Stone

Posted on

Solution to incomplete display of Scrollview nested ConstraintLayout or LinearLayout

The content exceeds screen's length,so use a ConstraintLayout wrap the contents up,then use a Scrollview nested outside,but after run on phone,the content can't display completely display.see the source code of Scrollview

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (!mFillViewport) {
            return;
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.UNSPECIFIED) {
            return;
        }

        if (getChildCount() > 0) {
            final View child = getChildAt(0);
            final int widthPadding;
            final int heightPadding;
            final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
            final FrameLayout.LayoutParams lp = (LayoutParams) child.getLayoutParams();
            if (targetSdkVersion >= VERSION_CODES.M) {
                widthPadding = mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin;
                heightPadding = mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin;
            } else {
                widthPadding = mPaddingLeft + mPaddingRight;
                heightPadding = mPaddingTop + mPaddingBottom;
            }

            final int desiredHeight = getMeasuredHeight() - heightPadding;
            if (child.getMeasuredHeight() < desiredHeight) {
                final int childWidthMeasureSpec = getChildMeasureSpec(
                        widthMeasureSpec, widthPadding, lp.width);
                final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
                        desiredHeight, MeasureSpec.EXACTLY);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
    }
Enter fullscreen mode Exit fullscreen mode

First of all,we see, when mfillview is false, it will be returned. The explanation of mfillview is: when this variable is set to true, Scrollview will make its child views fill in the currently visible area,I Try to add the attribute android:fillViewport under the Scrollview tag

android:fillViewport="true"
Enter fullscreen mode Exit fullscreen mode

or add

android:layout_marginBottom="xxdp"
Enter fullscreen mode Exit fullscreen mode

under the ConstraintLayout/Linearlayout

Top comments (0)