|
20 | 20 | import android.content.res.TypedArray; |
21 | 21 | import android.graphics.Canvas; |
22 | 22 | import android.graphics.Color; |
| 23 | +import android.graphics.Insets; |
23 | 24 | import android.graphics.Paint; |
24 | 25 | import android.util.AttributeSet; |
25 | 26 | import android.util.Log; |
@@ -559,9 +560,9 @@ static Alignment getAlignment(int gravity, boolean horizontal) { |
559 | 560 | int flags = (gravity & mask) >> shift; |
560 | 561 | switch (flags) { |
561 | 562 | case (AXIS_SPECIFIED | AXIS_PULL_BEFORE): |
562 | | - return LEADING; |
| 563 | + return horizontal ? LEFT : TOP; |
563 | 564 | case (AXIS_SPECIFIED | AXIS_PULL_AFTER): |
564 | | - return TRAILING; |
| 565 | + return horizontal ? RIGHT : BOTTOM; |
565 | 566 | case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER): |
566 | 567 | return FILL; |
567 | 568 | case AXIS_SPECIFIED: |
@@ -1042,12 +1043,15 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto |
1042 | 1043 | int rightMargin = getMargin(c, true, false); |
1043 | 1044 | int bottomMargin = getMargin(c, false, false); |
1044 | 1045 |
|
| 1046 | + int sumMarginsX = leftMargin + rightMargin; |
| 1047 | + int sumMarginsY = topMargin + bottomMargin; |
| 1048 | + |
1045 | 1049 | // Alignment offsets: the location of the view relative to its alignment group. |
1046 | | - int alignmentOffsetX = boundsX.getOffset(c, hAlign, leftMargin + pWidth + rightMargin); |
1047 | | - int alignmentOffsetY = boundsY.getOffset(c, vAlign, topMargin + pHeight + bottomMargin); |
| 1050 | + int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true); |
| 1051 | + int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false); |
1048 | 1052 |
|
1049 | | - int width = hAlign.getSizeInCell(c, pWidth, cellWidth - leftMargin - rightMargin); |
1050 | | - int height = vAlign.getSizeInCell(c, pHeight, cellHeight - topMargin - bottomMargin); |
| 1053 | + int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX); |
| 1054 | + int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY); |
1051 | 1055 |
|
1052 | 1056 | int dx = x1 + gravityOffsetX + alignmentOffsetX; |
1053 | 1057 |
|
@@ -1181,7 +1185,7 @@ private void computeGroupBounds() { |
1181 | 1185 | View c = getChildAt(i); |
1182 | 1186 | LayoutParams lp = getLayoutParams(c); |
1183 | 1187 | Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; |
1184 | | - groupBounds.getValue(i).include(c, spec, GridLayout.this, this); |
| 1188 | + groupBounds.getValue(i).include(GridLayout.this, c, spec, this); |
1185 | 1189 | } |
1186 | 1190 | } |
1187 | 1191 |
|
@@ -2138,16 +2142,30 @@ protected int size(boolean min) { |
2138 | 2142 | return before + after; |
2139 | 2143 | } |
2140 | 2144 |
|
2141 | | - protected int getOffset(View c, Alignment alignment, int size) { |
2142 | | - return before - alignment.getAlignmentValue(c, size); |
| 2145 | + private int getAlignmentValue(GridLayout gl, View c, int size, Alignment a, boolean horiz) { |
| 2146 | + boolean useLayoutBounds = gl.getLayoutMode() == LAYOUT_BOUNDS; |
| 2147 | + if (!useLayoutBounds) { |
| 2148 | + return a.getAlignmentValue(c, size); |
| 2149 | + } else { |
| 2150 | + Insets insets = c.getLayoutInsets(); |
| 2151 | + int leadingInset = horiz ? insets.left : insets.top; // RTL? |
| 2152 | + int trailingInset = horiz ? insets.right : insets.bottom; // RTL? |
| 2153 | + int totalInset = leadingInset + trailingInset; |
| 2154 | + return leadingInset + a.getAlignmentValue(c, size - totalInset); |
| 2155 | + } |
| 2156 | + } |
| 2157 | + |
| 2158 | + protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) { |
| 2159 | + return before - getAlignmentValue(gl, c, size, a, horizontal); |
2143 | 2160 | } |
2144 | 2161 |
|
2145 | | - protected final void include(View c, Spec spec, GridLayout gridLayout, Axis axis) { |
| 2162 | + protected final void include(GridLayout gl, View c, Spec spec, Axis axis) { |
2146 | 2163 | this.flexibility &= spec.getFlexibility(); |
2147 | | - int size = gridLayout.getMeasurementIncludingMargin(c, axis.horizontal); |
2148 | | - Alignment alignment = gridLayout.getAlignment(spec.alignment, axis.horizontal); |
| 2164 | + boolean horizontal = axis.horizontal; |
| 2165 | + int size = gl.getMeasurementIncludingMargin(c, horizontal); |
| 2166 | + Alignment alignment = gl.getAlignment(spec.alignment, horizontal); |
2149 | 2167 | // todo test this works correctly when the returned value is UNDEFINED |
2150 | | - int before = alignment.getAlignmentValue(c, size); |
| 2168 | + int before = getAlignmentValue(gl, c, size, alignment, horizontal); |
2151 | 2169 | include(before, size - before); |
2152 | 2170 | } |
2153 | 2171 |
|
@@ -2614,8 +2632,8 @@ protected int size(boolean min) { |
2614 | 2632 | } |
2615 | 2633 |
|
2616 | 2634 | @Override |
2617 | | - protected int getOffset(View c, Alignment alignment, int size) { |
2618 | | - return max(0, super.getOffset(c, alignment, size)); |
| 2635 | + protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean hrz) { |
| 2636 | + return max(0, super.getOffset(gl, c, a, size, hrz)); |
2619 | 2637 | } |
2620 | 2638 | }; |
2621 | 2639 | } |
|
0 commit comments