@@ -581,10 +581,10 @@ private int getDefaultMargin(View c, boolean horizontal, boolean leading) {
581581 }
582582
583583 private int getDefaultMargin (View c , boolean isAtEdge , boolean horizontal , boolean leading ) {
584- return isAtEdge ? DEFAULT_CONTAINER_MARGIN : getDefaultMargin (c , horizontal , leading );
584+ return /* isAtEdge ? DEFAULT_CONTAINER_MARGIN :*/ getDefaultMargin (c , horizontal , leading );
585585 }
586586
587- private int getDefaultMarginValue (View c , LayoutParams p , boolean horizontal , boolean leading ) {
587+ private int getDefaultMargin (View c , LayoutParams p , boolean horizontal , boolean leading ) {
588588 if (!useDefaultMargins ) {
589589 return 0 ;
590590 }
@@ -602,7 +602,7 @@ int getMargin1(View view, boolean horizontal, boolean leading) {
602602 int margin = horizontal ?
603603 (leading ? lp .leftMargin : lp .rightMargin ) :
604604 (leading ? lp .topMargin : lp .bottomMargin );
605- return margin == UNDEFINED ? getDefaultMarginValue (view , lp , horizontal , leading ) : margin ;
605+ return margin == UNDEFINED ? getDefaultMargin (view , lp , horizontal , leading ) : margin ;
606606 }
607607
608608 private int getMargin (View view , boolean horizontal , boolean leading ) {
@@ -777,11 +777,12 @@ protected void onDebugDrawMargins(Canvas canvas) {
777777 LayoutParams lp = new LayoutParams ();
778778 for (int i = 0 ; i < getChildCount (); i ++) {
779779 View c = getChildAt (i );
780+ Insets insets = getLayoutMode () == OPTICAL_BOUNDS ? c .getOpticalInsets () : Insets .NONE ;
780781 lp .setMargins (
781- getMargin1 (c , true , true ),
782- getMargin1 (c , false , true ),
783- getMargin1 (c , true , false ),
784- getMargin1 (c , false , false ));
782+ getMargin1 (c , true , true ) - insets . left ,
783+ getMargin1 (c , false , true ) - insets . top ,
784+ getMargin1 (c , true , false ) - insets . right ,
785+ getMargin1 (c , false , false ) - insets . bottom );
785786 lp .onDebugDraw (c , canvas );
786787 }
787788 }
@@ -946,7 +947,12 @@ protected void onMeasure(int widthSpec, int heightSpec) {
946947 }
947948
948949 private int getMeasurement (View c , boolean horizontal ) {
949- return horizontal ? c .getMeasuredWidth () : c .getMeasuredHeight ();
950+ int result = horizontal ? c .getMeasuredWidth () : c .getMeasuredHeight ();
951+ if (getLayoutMode () == OPTICAL_BOUNDS ) {
952+ Insets insets = c .getOpticalInsets ();
953+ return result - (horizontal ? insets .left + insets .right : insets .top + insets .bottom );
954+ }
955+ return result ;
950956 }
951957
952958 final int getMeasurementIncludingMargin (View c , boolean horizontal ) {
@@ -1052,6 +1058,14 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
10521058 targetWidth - width - paddingRight - rightMargin - dx ;
10531059 int cy = paddingTop + y1 + gravityOffsetY + alignmentOffsetY + topMargin ;
10541060
1061+ boolean useLayoutBounds = getLayoutMode () == OPTICAL_BOUNDS ;
1062+ if (useLayoutBounds ) {
1063+ Insets insets = c .getOpticalInsets ();
1064+ cx -= insets .left ;
1065+ cy -= insets .top ;
1066+ width += (insets .left + insets .right );
1067+ height += (insets .top + insets .bottom );
1068+ }
10551069 if (width != c .getMeasuredWidth () || height != c .getMeasuredHeight ()) {
10561070 c .measure (makeMeasureSpec (width , EXACTLY ), makeMeasureSpec (height , EXACTLY ));
10571071 }
@@ -2135,21 +2149,8 @@ protected int size(boolean min) {
21352149 return before + after ;
21362150 }
21372151
2138- private int getAlignmentValue (GridLayout gl , View c , int size , Alignment a , boolean horiz ) {
2139- boolean useLayoutBounds = gl .getLayoutMode () == LAYOUT_BOUNDS ;
2140- if (!useLayoutBounds ) {
2141- return a .getAlignmentValue (c , size );
2142- } else {
2143- Insets insets = c .getLayoutInsets ();
2144- int leadingInset = horiz ? insets .left : insets .top ; // RTL?
2145- int trailingInset = horiz ? insets .right : insets .bottom ; // RTL?
2146- int totalInset = leadingInset + trailingInset ;
2147- return leadingInset + a .getAlignmentValue (c , size - totalInset );
2148- }
2149- }
2150-
21512152 protected int getOffset (GridLayout gl , View c , Alignment a , int size , boolean horizontal ) {
2152- return before - getAlignmentValue (gl , c , size , a , horizontal );
2153+ return before - a . getAlignmentValue (c , size , gl . getLayoutMode () );
21532154 }
21542155
21552156 protected final void include (GridLayout gl , View c , Spec spec , Axis axis ) {
@@ -2158,7 +2159,7 @@ protected final void include(GridLayout gl, View c, Spec spec, Axis axis) {
21582159 int size = gl .getMeasurementIncludingMargin (c , horizontal );
21592160 Alignment alignment = gl .getAlignment (spec .alignment , horizontal );
21602161 // todo test this works correctly when the returned value is UNDEFINED
2161- int before = getAlignmentValue (gl , c , size , alignment , horizontal );
2162+ int before = alignment . getAlignmentValue (c , size , gl . getLayoutMode () );
21622163 include (before , size - before );
21632164 }
21642165
@@ -2441,9 +2442,10 @@ public static abstract class Alignment {
24412442 *
24422443 * @param view the view to which this alignment should be applied
24432444 * @param viewSize the measured size of the view
2445+ * @param mode the basis of alignment: CLIP or OPTICAL
24442446 * @return the alignment value
24452447 */
2446- abstract int getAlignmentValue (View view , int viewSize );
2448+ abstract int getAlignmentValue (View view , int viewSize , int mode );
24472449
24482450 /**
24492451 * Returns the size of the view specified by this alignment.
@@ -2473,7 +2475,7 @@ int getGravityOffset(View view, int cellDelta) {
24732475 }
24742476
24752477 @ Override
2476- public int getAlignmentValue (View view , int viewSize ) {
2478+ public int getAlignmentValue (View view , int viewSize , int mode ) {
24772479 return UNDEFINED ;
24782480 }
24792481 };
@@ -2489,7 +2491,7 @@ int getGravityOffset(View view, int cellDelta) {
24892491 }
24902492
24912493 @ Override
2492- public int getAlignmentValue (View view , int viewSize ) {
2494+ public int getAlignmentValue (View view , int viewSize , int mode ) {
24932495 return 0 ;
24942496 }
24952497 };
@@ -2505,7 +2507,7 @@ int getGravityOffset(View view, int cellDelta) {
25052507 }
25062508
25072509 @ Override
2508- public int getAlignmentValue (View view , int viewSize ) {
2510+ public int getAlignmentValue (View view , int viewSize , int mode ) {
25092511 return viewSize ;
25102512 }
25112513 };
@@ -2542,8 +2544,8 @@ int getGravityOffset(View view, int cellDelta) {
25422544 }
25432545
25442546 @ Override
2545- public int getAlignmentValue (View view , int viewSize ) {
2546- return (!view .isLayoutRtl () ? ltr : rtl ).getAlignmentValue (view , viewSize );
2547+ public int getAlignmentValue (View view , int viewSize , int mode ) {
2548+ return (!view .isLayoutRtl () ? ltr : rtl ).getAlignmentValue (view , viewSize , mode );
25472549 }
25482550 };
25492551 }
@@ -2572,7 +2574,7 @@ int getGravityOffset(View view, int cellDelta) {
25722574 }
25732575
25742576 @ Override
2575- public int getAlignmentValue (View view , int viewSize ) {
2577+ public int getAlignmentValue (View view , int viewSize , int mode ) {
25762578 return viewSize >> 1 ;
25772579 }
25782580 };
@@ -2591,9 +2593,16 @@ int getGravityOffset(View view, int cellDelta) {
25912593 }
25922594
25932595 @ Override
2594- public int getAlignmentValue (View view , int viewSize ) {
2596+ public int getAlignmentValue (View view , int viewSize , int mode ) {
25952597 int baseline = view .getBaseline ();
2596- return (baseline == -1 ) ? UNDEFINED : baseline ;
2598+ if (baseline == -1 ) {
2599+ return UNDEFINED ;
2600+ } else {
2601+ if (mode == OPTICAL_BOUNDS ) {
2602+ return baseline - view .getOpticalInsets ().top ;
2603+ }
2604+ return baseline ;
2605+ }
25972606 }
25982607
25992608 @ Override
@@ -2644,7 +2653,7 @@ int getGravityOffset(View view, int cellDelta) {
26442653 }
26452654
26462655 @ Override
2647- public int getAlignmentValue (View view , int viewSize ) {
2656+ public int getAlignmentValue (View view , int viewSize , int mode ) {
26482657 return UNDEFINED ;
26492658 }
26502659
0 commit comments