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

Commit 5b1b2417 authored by Romain Guy's avatar Romain Guy
Browse files

Add a new attribute to LinearLayout: useLargestChild.

Bug #2379138

This new attribute is to be used when the LinearLayout is wrap_content but you want
all the children inside to have the same dimension. Set useLargestChild to true and
give all the children a width/height of 0 and a weight of 1.0.
parent 051ab623
Loading
Loading
Loading
Loading
+89 −20
Original line number Diff line number Diff line
@@ -94,6 +94,9 @@ public class LinearLayout extends ViewGroup {
    @ViewDebug.ExportedProperty
    private float mWeightSum;

    @ViewDebug.ExportedProperty
    private boolean mUseLargestChild;

    private int[] mMaxAscent;
    private int[] mMaxDescent;

@@ -134,6 +137,9 @@ public class LinearLayout extends ViewGroup {
        mBaselineAlignedChildIndex =
                a.getInt(com.android.internal.R.styleable.LinearLayout_baselineAlignedChildIndex, -1);

        // TODO: Better name, add Java APIs, make it public
        mUseLargestChild = a.getBoolean(R.styleable.LinearLayout_useLargestChild, false);

        a.recycle();
    }

@@ -328,6 +334,9 @@ public class LinearLayout extends ViewGroup {
        boolean matchWidth = false;

        final int baselineChildIndex = mBaselineAlignedChildIndex;        
        final boolean useLargestChild = mUseLargestChild;

        int largestChildHeight = Integer.MIN_VALUE;

        // See how tall everyone is. Also remember max width.
        for (int i = 0; i < count; ++i) {
@@ -375,8 +384,13 @@ public class LinearLayout extends ViewGroup {
                   lp.height = oldHeight;
                }

               mTotalLength += child.getMeasuredHeight() + lp.topMargin +
                final int childHeight = child.getMeasuredHeight();
                mTotalLength += childHeight + lp.topMargin +
                       lp.bottomMargin + getNextLocationOffset(child);

                if (useLargestChild) {
                    largestChildHeight = Math.max(childHeight, largestChildHeight);
                }
            }

            /**
@@ -427,6 +441,29 @@ public class LinearLayout extends ViewGroup {
            i += getChildrenSkipCount(child, i);
        }

        if (useLargestChild) {
            mTotalLength = 0;

            for (int i = 0; i < count; ++i) {
                final View child = getVirtualChildAt(i);

                if (child == null) {
                    mTotalLength += measureNullChild(i);
                    continue;
                }

                if (child.getVisibility() == GONE) {
                    i += getChildrenSkipCount(child, i);
                    continue;
                }

                final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
                        child.getLayoutParams();
                mTotalLength += largestChildHeight + lp.topMargin+ lp.bottomMargin +
                        getNextLocationOffset(child);
            }
        }

        // Add in our padding
        mTotalLength += mPaddingTop + mPaddingBottom;

@@ -587,6 +624,9 @@ public class LinearLayout extends ViewGroup {
        maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1;

        final boolean baselineAligned = mBaselineAligned;
        final boolean useLargestChild = mUseLargestChild;

        int largestChildWidth = Integer.MIN_VALUE;

        // See how wide everyone is. Also remember max height.
        for (int i = 0; i < count; ++i) {
@@ -602,7 +642,8 @@ public class LinearLayout extends ViewGroup {
                continue;
            }

            final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
            final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
                    child.getLayoutParams();

            totalWeight += lp.weight;
            
@@ -644,8 +685,13 @@ public class LinearLayout extends ViewGroup {
                    lp.width = oldWidth;
                }

                mTotalLength += child.getMeasuredWidth() + lp.leftMargin +
                        lp.rightMargin + getNextLocationOffset(child);
                final int childWidth = child.getMeasuredWidth();
                mTotalLength += childWidth + lp.leftMargin + lp.rightMargin +
                        getNextLocationOffset(child);

                if (useLargestChild) {
                    largestChildWidth = Math.max(childWidth, largestChildWidth);
                }
            }

            boolean matchHeightLocally = false;
@@ -708,6 +754,29 @@ public class LinearLayout extends ViewGroup {
            maxHeight = Math.max(maxHeight, ascent + descent);
        }

        if (useLargestChild) {
            mTotalLength = 0;

            for (int i = 0; i < count; ++i) {
                final View child = getVirtualChildAt(i);

                if (child == null) {
                    mTotalLength += measureNullChild(i);
                    continue;
                }

                if (child.getVisibility() == GONE) {
                    i += getChildrenSkipCount(child, i);
                    continue;
                }

                final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
                        child.getLayoutParams();
                mTotalLength += largestChildWidth + lp.leftMargin + lp.rightMargin +
                        getNextLocationOffset(child);
            }
        }

        // Add in our padding
        mTotalLength += mPaddingLeft + mPaddingRight;
        
@@ -953,7 +1022,7 @@ public class LinearLayout extends ViewGroup {
        final int paddingLeft = mPaddingLeft;

        int childTop = mPaddingTop;
        int childLeft = paddingLeft;
        int childLeft;
        
        // Where right end of child should go
        final int width = mRight - mLeft;
@@ -1038,7 +1107,7 @@ public class LinearLayout extends ViewGroup {
    void layoutHorizontal() {
        final int paddingTop = mPaddingTop;

        int childTop = paddingTop;
        int childTop;
        int childLeft = mPaddingLeft;
        
        // Where bottom of child should go
@@ -1103,7 +1172,7 @@ public class LinearLayout extends ViewGroup {
                        break;

                    case Gravity.CENTER_VERTICAL:
                        // Removed support for baselign alignment when layout_gravity or
                        // Removed support for baseline alignment when layout_gravity or
                        // gravity == center_vertical. See bug #1038483.
                        // Keep the code around if we need to re-enable this feature
                        // if (childBaseline != -1) {
+2 −1
Original line number Diff line number Diff line
@@ -109,7 +109,8 @@
            android:orientation="horizontal"
            android:paddingTop="4dip"
            android:paddingLeft="2dip"
            android:paddingRight="2dip" >
            android:paddingRight="2dip"
            android:useLargestChild="true">
            <LinearLayout android:id="@+id/leftSpacer"
                android:layout_weight="0.25"
                android:layout_width="0dip"
+4 −0
Original line number Diff line number Diff line
@@ -1700,6 +1700,10 @@
             space by giving it a layout_weight of 0.5 and setting the weightSum
             to 1.0. -->
        <attr name="weightSum" format="float" />
        <!-- When set to true, all children with a weight will be considered having
             the minimum size of the largest child. If false, all children are
             measured normally. -->
        <attr name="useLargestChild" format="boolean" />
    </declare-styleable>
    <declare-styleable name="ListView">
        <!-- Reference to an array resource that will populate the ListView.  For static content,