From cc26636f08da89f9750f550d66aa213f1ae24f7f Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Wed, 4 May 2016 16:59:38 -0400 Subject: Always assign leftover pixels to last weighted child Bug: 27690033 Change-Id: Ibee51d49d3f7aa923750ce330df2e1aa2156f2bd --- core/java/android/widget/LinearLayout.java | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/core/java/android/widget/LinearLayout.java b/core/java/android/widget/LinearLayout.java index f75b74bb9a14..0b73afb657f7 100644 --- a/core/java/android/widget/LinearLayout.java +++ b/core/java/android/widget/LinearLayout.java @@ -708,6 +708,7 @@ public class LinearLayout extends ViewGroup { final int baselineChildIndex = mBaselineAlignedChildIndex; final boolean useLargestChild = mUseLargestChild; + int lastWeightedChildIndex = -1; int largestChildHeight = Integer.MIN_VALUE; int consumedExcessSpace = 0; @@ -729,8 +730,10 @@ public class LinearLayout extends ViewGroup { } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); - - totalWeight += lp.weight; + if (lp.weight > 0) { + totalWeight += lp.weight; + lastWeightedChildIndex = i; + } final boolean useExcessSpace = lp.height == 0 && lp.weight > 0; if (heightMode == MeasureSpec.EXACTLY && useExcessSpace) { @@ -885,7 +888,13 @@ public class LinearLayout extends ViewGroup { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final float childWeight = lp.weight; if (childWeight > 0) { - final int share = (int) (childWeight * remainingExcess / remainingWeightSum); + final int share; + if (i == lastWeightedChildIndex) { + share = remainingExcess; + } else { + share = (int) (childWeight * remainingExcess / remainingWeightSum); + } + remainingExcess -= share; remainingWeightSum -= childWeight; @@ -1046,6 +1055,7 @@ public class LinearLayout extends ViewGroup { final boolean isExactly = widthMode == MeasureSpec.EXACTLY; + int lastWeightedChildIndex = -1; int largestChildWidth = Integer.MIN_VALUE; int usedExcessSpace = 0; @@ -1067,8 +1077,10 @@ public class LinearLayout extends ViewGroup { } final LayoutParams lp = (LayoutParams) child.getLayoutParams(); - - totalWeight += lp.weight; + if (lp.weight > 0) { + totalWeight += lp.weight; + lastWeightedChildIndex = i; + } final boolean useExcessSpace = lp.width == 0 && lp.weight > 0; if (widthMode == MeasureSpec.EXACTLY && useExcessSpace) { @@ -1267,7 +1279,13 @@ public class LinearLayout extends ViewGroup { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final float childWeight = lp.weight; if (childWeight > 0) { - final int share = (int) (childWeight * remainingExcess / remainingWeightSum); + final int share; + if (i == lastWeightedChildIndex) { + share = remainingExcess; + } else { + share = (int) (childWeight * remainingExcess / remainingWeightSum); + } + remainingExcess -= share; remainingWeightSum -= childWeight; -- cgit v1.2.3-59-g8ed1b