Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76d59a3b authored by Romain Guy's avatar Romain Guy
Browse files

Always take padding and margins into account

Bug #8565842

Change-Id: I8ee398b5c36b3011950265eb7e22cc8338f1aeee
parent 50b9eb1c
Loading
Loading
Loading
Loading
+21 −9
Original line number Diff line number Diff line
@@ -220,28 +220,29 @@ public class RelativeLayout extends ViewGroup {
    // with MeasureSpec value overflow and RelativeLayout was one source of them.
    // Some apps came to rely on them. :(
    private boolean mAllowBrokenMeasureSpecs = false;
    // Compatibility hack. Old versions of the platform would not take
    // margins and padding into account when generating the height measure spec
    // for children during the horizontal measure pass.
    private boolean mMeasureVerticalWithPaddingMargin = false;

    // A default width used for RTL measure pass
    private static int DEFAULT_WIDTH = Integer.MAX_VALUE / 2;
    private static final int DEFAULT_WIDTH = Integer.MAX_VALUE / 2;

    public RelativeLayout(Context context) {
        super(context);
        mAllowBrokenMeasureSpecs = context.getApplicationInfo().targetSdkVersion <=
                Build.VERSION_CODES.JELLY_BEAN_MR1;
        queryCompatibilityModes(context);
    }

    public RelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initFromAttributes(context, attrs);
        mAllowBrokenMeasureSpecs = context.getApplicationInfo().targetSdkVersion <=
                Build.VERSION_CODES.JELLY_BEAN_MR1;
        queryCompatibilityModes(context);
    }

    public RelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initFromAttributes(context, attrs);
        mAllowBrokenMeasureSpecs = context.getApplicationInfo().targetSdkVersion <=
                Build.VERSION_CODES.JELLY_BEAN_MR1;
        queryCompatibilityModes(context);
    }

    private void initFromAttributes(Context context, AttributeSet attrs) {
@@ -251,6 +252,12 @@ public class RelativeLayout extends ViewGroup {
        a.recycle();
    }

    private void queryCompatibilityModes(Context context) {
        int version = context.getApplicationInfo().targetSdkVersion;
        mAllowBrokenMeasureSpecs = version <= Build.VERSION_CODES.JELLY_BEAN_MR1;
        mMeasureVerticalWithPaddingMargin = version >= Build.VERSION_CODES.JELLY_BEAN_MR2;
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
@@ -692,6 +699,11 @@ public class RelativeLayout extends ViewGroup {
                params.leftMargin, params.rightMargin,
                mPaddingLeft, mPaddingRight,
                myWidth);
        int maxHeight = myHeight;
        if (mMeasureVerticalWithPaddingMargin) {
            maxHeight = Math.max(0, myHeight - mPaddingTop - mPaddingBottom -
                    params.topMargin - params.bottomMargin);
        }
        int childHeightMeasureSpec;
        if (myHeight < 0 && !mAllowBrokenMeasureSpecs) {
            if (params.height >= 0) {
@@ -704,9 +716,9 @@ public class RelativeLayout extends ViewGroup {
                childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            }
        } else if (params.width == LayoutParams.MATCH_PARENT) {
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(myHeight, MeasureSpec.EXACTLY);
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY);
        } else {
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(myHeight, MeasureSpec.AT_MOST);
            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
        }
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }