diff --git a/api/current.txt b/api/current.txt index 807cfd6ad7806..9de395b643df9 100644 --- a/api/current.txt +++ b/api/current.txt @@ -749,6 +749,7 @@ package android { field public static final int layout_centerVertical = 16843153; // 0x1010191 field public static final int layout_column = 16843084; // 0x101014c field public static final int layout_columnSpan = 16843645; // 0x101037d + field public static final int layout_columnWeight = 16843868; // 0x101045c field public static final int layout_gravity = 16842931; // 0x10100b3 field public static final int layout_height = 16842997; // 0x10100f5 field public static final int layout_margin = 16842998; // 0x10100f6 @@ -760,6 +761,7 @@ package android { field public static final int layout_marginTop = 16843000; // 0x10100f8 field public static final int layout_row = 16843643; // 0x101037b field public static final int layout_rowSpan = 16843644; // 0x101037c + field public static final int layout_rowWeight = 16843867; // 0x101045b field public static final int layout_scale = 16843155; // 0x1010193 field public static final int layout_span = 16843085; // 0x101014d field public static final int layout_toEndOf = 16843704; // 0x10103b8 @@ -36576,6 +36578,10 @@ package android.widget { method public void setRowCount(int); method public void setRowOrderPreserved(boolean); method public void setUseDefaultMargins(boolean); + method public static android.widget.GridLayout.Spec spec(int, int, android.widget.GridLayout.Alignment, float); + method public static android.widget.GridLayout.Spec spec(int, android.widget.GridLayout.Alignment, float); + method public static android.widget.GridLayout.Spec spec(int, int, float); + method public static android.widget.GridLayout.Spec spec(int, float); method public static android.widget.GridLayout.Spec spec(int, int, android.widget.GridLayout.Alignment); method public static android.widget.GridLayout.Spec spec(int, android.widget.GridLayout.Alignment); method public static android.widget.GridLayout.Spec spec(int, int); diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java index 851160163e59a..97f89aaf82233 100644 --- a/core/java/android/widget/GridLayout.java +++ b/core/java/android/widget/GridLayout.java @@ -104,14 +104,16 @@ import static java.lang.Math.min; * *
- * A child's ability to stretch is inferred from the alignment properties of - * its row and column groups (which are typically set by setting the - * {@link LayoutParams#setGravity(int) gravity} property of the child's layout parameters). - * If alignment was defined along a given axis then the component - * is taken as flexible in that direction. If no alignment was set, + * The flexibility of a view is therefore influenced by its alignment which is, + * in turn, typically defined by setting the + * {@link LayoutParams#setGravity(int) gravity} property of the child's layout parameters. + * If either a weight or alignment were defined along a given axis then the component + * is taken as flexible in that direction. If no weight or alignment was set, * the component is instead assumed to be inflexible. *
* Multiple components in the same row or column group are @@ -122,12 +124,16 @@ import static java.lang.Math.min; * elements is flexible if one of its elements is flexible. *
* To make a column stretch, make sure all of the components inside it define a - * gravity. To prevent a column from stretching, ensure that one of the components - * in the column does not define a gravity. + * weight or a gravity. To prevent a column from stretching, ensure that one of the components + * in the column does not define a weight or a gravity. *
* When the principle of flexibility does not provide complete disambiguation, * GridLayout's algorithms favour rows and columns that are closer to its right - * and bottom edges. + * and bottom edges. To be more precise, GridLayout treats each of its layout + * parameters as a constraint in the a set of variables that define the grid-lines along a + * given axis. During layout, GridLayout solves the constraints so as to return the unique + * solution to those constraints for which all variables are less-than-or-equal-to + * the corresponding value in any other valid solution. * *
- * Some common use-cases may nevertheless be accommodated as follows. - * To place equal amounts of space around a component in a cell group; - * use {@link #CENTER} alignment (or {@link LayoutParams#setGravity(int) gravity}). - * For complete control over excess space distribution in a row or column; - * use a {@link LinearLayout} subview to hold the components in the associated cell group. - * When using either of these techniques, bear in mind that cell groups may be defined to overlap. *
* See {@link GridLayout.LayoutParams} for a full description of the
* layout parameters used by GridLayout.
@@ -1693,8 +1687,74 @@ public class GridLayout extends ViewGroup {
return trailingMargins;
}
- private void computeLocations(int[] a) {
+ private void solve(int[] a) {
solve(getArcs(), a);
+ }
+
+ private void resetDeltas() {
+ Bounds[] values = getGroupBounds().values;
+ for (int i = 0, N = values.length; i < N; i++) {
+ values[i].shareOfDelta = 0;
+ }
+ }
+
+ private boolean requiresWeightDistribution() {
+ Bounds[] values = getGroupBounds().values;
+ for (int i = 0, N = values.length; i < N; i++) {
+ if (values[i].weight != 0) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private void shareOutDelta(int[] locations) {
+ PackedMap
+ * The weight property is also included in Spec and specifies the proportion of any
+ * excess space that is due to the enclosing row or column.
+ * GridLayout's model of flexibility and weight is broadly based on the physical properties of
+ * systems of mechanical springs. For example, a column in a GridLayout is modeled as
+ * if a set of springs were attached in parallel at their ends. Columns are therefore
+ * flexible only if and only if all views inside them are flexible. Similarly, the combined
+ * weight of a column is defined as the minimum of the weights of the components it
+ * contains. So, if any one component in a column of components has a weight of zero,
+ * the entire group has a weight of zero and will not receive any of the
+ * excess space that GridLayout distributes in its second
+ * (internal) layout pass. The default weight of a component is zero,
+ * reflecting inflexibility, but the default weight of a column (with nothing in it)
+ * is effectively infinite, reflecting the fact that a parallel group of
+ * springs is infinitely flexible when it contains no springs.
+ *
+ * The above comments apply equally to rows and groups of rows and columns.
*
*
* To leave the start index undefined, use the value {@link #UNDEFINED}.
@@ -2485,9 +2582,55 @@ public class GridLayout extends ViewGroup {
* @param start the start
* @param size the size
* @param alignment the alignment
+ * @param weight the weight
+ */
+ public static Spec spec(int start, int size, Alignment alignment, float weight) {
+ return new Spec(start != UNDEFINED, start, size, alignment, weight);
+ }
+
+ /**
+ * Equivalent to: {@code spec(start, 1, alignment, weight)}.
+ *
+ * @param start the start
+ * @param alignment the alignment
+ * @param weight the weight
+ */
+ public static Spec spec(int start, Alignment alignment, float weight) {
+ return spec(start, 1, alignment, weight);
+ }
+
+ /**
+ * Equivalent to: {@code spec(start, 1, default_alignment, weight)} -
+ * where {@code default_alignment} is specified in
+ * {@link android.widget.GridLayout.LayoutParams}.
+ *
+ * @param start the start
+ * @param size the size
+ * @param weight the weight
+ */
+ public static Spec spec(int start, int size, float weight) {
+ return spec(start, size, UNDEFINED_ALIGNMENT, weight);
+ }
+
+ /**
+ * Equivalent to: {@code spec(start, 1, weight)}.
+ *
+ * @param start the start
+ * @param weight the weight
+ */
+ public static Spec spec(int start, float weight) {
+ return spec(start, 1, weight);
+ }
+
+ /**
+ * Equivalent to: {@code spec(start, size, alignment, 0f)}.
+ *
+ * @param start the start
+ * @param size the size
+ * @param alignment the alignment
*/
public static Spec spec(int start, int size, Alignment alignment) {
- return new Spec(start != UNDEFINED, start, size, alignment);
+ return spec(start, size, alignment, Spec.DEFAULT_WEIGHT);
}
/**
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index c05dfcab5b869..9eb6f7426595d 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -4057,6 +4057,9 @@
The default is one.
See {@link android.widget.GridLayout.Spec}. -->
WRAP_CONTENT and MATCH_PARENT
*
@@ -1851,9 +1928,11 @@ public class GridLayout extends ViewGroup {
* .row = {@link #UNDEFINED} .rowSpan = 1 .alignment = {@link #BASELINE} .weight = 0 .column = {@link #UNDEFINED} .columnSpan = 1 .alignment = {@link #START} .weight = 0
*
*