diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java index bda82a3842543..092c2f75d66aa 100644 --- a/core/java/android/widget/GridLayout.java +++ b/core/java/android/widget/GridLayout.java @@ -494,6 +494,14 @@ public class GridLayout extends ViewGroup { requestLayout(); } + private static int max2(int[] a, int valueIfEmpty) { + int result = valueIfEmpty; + for (int i = 0, N = a.length; i < N; i++) { + result = Math.max(result, a[i]); + } + return result; + } + private static int sum(float[] a) { int result = 0; for (int i = 0, length = a.length; i < length; i++) { @@ -1409,7 +1417,7 @@ public class GridLayout extends ViewGroup { // External entry points private int size(int[] locations) { - return locations[locations.length - 1] - locations[0]; + return max2(locations, 0) - locations[0]; } private int getMin() { @@ -1878,21 +1886,13 @@ public class GridLayout extends ViewGroup { return result; } - private static int max(int[] a, int valueIfEmpty) { - int result = valueIfEmpty; - for (int i = 0, length = a.length; i < length; i++) { - result = Math.max(result, a[i]); - } - return result; - } - /* Create a compact array of keys or values using the supplied index. */ private static K[] compact(K[] a, int[] index) { int size = a.length; Class componentType = a.getClass().getComponentType(); - K[] result = (K[]) Array.newInstance(componentType, max(index, -1) + 1); + K[] result = (K[]) Array.newInstance(componentType, max2(index, -1) + 1); // this overwrite duplicates, retaining the last equivalent entry for (int i = 0; i < size; i++) { diff --git a/tests/GridLayoutTest/res/layout/grid3.xml b/tests/GridLayoutTest/res/layout/grid3.xml index 31dc75abfb9c4..5cdacf74faa5a 100644 --- a/tests/GridLayoutTest/res/layout/grid3.xml +++ b/tests/GridLayoutTest/res/layout/grid3.xml @@ -19,14 +19,17 @@ android:layout_width="match_parent" android:layout_height="match_parent" + android:useDefaultMargins="true" android:marginsIncludedInAlignment="false" + android:columnCount="4" > @@ -34,12 +37,14 @@ @@ -49,6 +54,7 @@ @@ -58,14 +64,15 @@ />