Skip to content Skip to sidebar Skip to footer

Widthmeasurespec Is 0 When In Horizontalscrollview

I'm writing a custom view in Android. Since I need precise size management, I've overridden onMeasure. Its documentation says, that View.MeasureSpec.getMode returns one of three va

Solution 1:

0 means the mode is UNSPECIFIED. This means you can be as big as you want, which makes sense since it is a ScrollView...intended for a View bigger than your actual screen.

Being unspecified, you don't have to care about the size, this is why it is 0.

If you look at the source of HorizontalScrollView you can also see that it just passes width: 0, UNSPECIFIED to the child:

@OverrideprotectedvoidmeasureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
    ViewGroup.LayoutParamslp= child.getLayoutParams();

    int childWidthMeasureSpec;
    int childHeightMeasureSpec;

    childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, mPaddingTop
            + mPaddingBottom, lp.height);

    childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

To get the child view as big as the parent you can use fillViewPort from xml or java which will lead to a call with mode EXACTLY:

if (!mFillViewport) {
    return;
}
// ...int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);

This is one way to handle your child view being as big as the ScrollView.

Post a Comment for "Widthmeasurespec Is 0 When In Horizontalscrollview"