diff options
author | Philip Milne <pmilne@google.com> | 2012-04-16 10:03:17 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-04-16 10:03:17 -0700 |
commit | 4e1cb3b7942d3ca05918604358b4ef83815ce448 (patch) | |
tree | 8c4d1b32367dda945548a3d1a1b895ad39a3ac03 /core | |
parent | 7c886acf2e60d7f4a02197401d1d046ef3b870b4 (diff) | |
parent | 1557fd7809078e421f751efc7d2539b3efdc54b2 (diff) | |
download | frameworks_base-4e1cb3b7942d3ca05918604358b4ef83815ce448.zip frameworks_base-4e1cb3b7942d3ca05918604358b4ef83815ce448.tar.gz frameworks_base-4e1cb3b7942d3ca05918604358b4ef83815ce448.tar.bz2 |
Merge "Fix for bug 6110465."
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/view/View.java | 30 | ||||
-rw-r--r-- | core/java/android/view/ViewGroup.java | 70 | ||||
-rw-r--r-- | core/java/android/widget/GridLayout.java | 48 |
3 files changed, 133 insertions, 15 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 962e13a..d569ba1 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -24,6 +24,7 @@ import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Camera; import android.graphics.Canvas; +import android.graphics.Insets; import android.graphics.Interpolator; import android.graphics.LinearGradient; import android.graphics.Matrix; @@ -2698,6 +2699,12 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal protected int mPaddingBottom; /** + * The layout insets in pixels, that is the distance in pixels between the + * visible edges of this view its bounds. + */ + private Insets mLayoutInsets; + + /** * Briefly describes the view and is primarily used for accessibility support. */ private CharSequence mContentDescription; @@ -13842,6 +13849,29 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal } /** + * @hide + */ + public Insets getLayoutInsets() { + if (mLayoutInsets == null) { + if (mBackground == null) { + mLayoutInsets = Insets.NONE; + } else { + Rect insetRect = new Rect(); + boolean hasInsets = mBackground.getLayoutInsets(insetRect); + mLayoutInsets = hasInsets ? Insets.of(insetRect) : Insets.NONE; + } + } + return mLayoutInsets; + } + + /** + * @hide + */ + public void setLayoutInsets(Insets layoutInsets) { + mLayoutInsets = layoutInsets; + } + + /** * Changes the selection state of this view. A view can be selected or not. * Note that selection is not the same as focus. Views are typically * selected in the context of an AdapterView like ListView or GridView; diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 7e90e2b..ff8e66a 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -170,6 +170,12 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager */ protected int mGroupFlags; + /* + * THe layout mode: either {@link #UNDEFINED_LAYOUT_MODE}, {@link #COMPONENT_BOUNDS} or + * {@link #LAYOUT_BOUNDS} + */ + private int mLayoutMode = UNDEFINED_LAYOUT_MODE; + /** * NOTE: If you change the flags below make sure to reflect the changes * the DisplayList class @@ -335,6 +341,24 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager */ public static final int PERSISTENT_ALL_CACHES = 0x3; + // Layout Modes + + private static final int UNDEFINED_LAYOUT_MODE = -1; + + /** + * This constant is a {@link #setLayoutMode(int) layoutMode}. + * Component bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top}, + * {@link #getRight() right} and {@link #getBottom() bottom}. + */ + public static final int COMPONENT_BOUNDS = 0; + + /** + * This constant is a {@link #setLayoutMode(int) layoutMode}. + * Layout bounds are derived by offsetting the component bounds using + * {@link View#getLayoutInsets()}. + */ + public static final int LAYOUT_BOUNDS = 1; + /** * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL * are set at the same time. @@ -4424,6 +4448,52 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager } /** + * Returns the basis of alignment during the layout of this view group: + * either {@link #COMPONENT_BOUNDS} or {@link #LAYOUT_BOUNDS}. + * + * @return whether or not this view group should use the component or layout bounds during + * layout operations + * + * @see #setLayoutMode(int) + */ + public int getLayoutMode() { + if (mLayoutMode == UNDEFINED_LAYOUT_MODE) { + ViewParent parent = getParent(); + if (parent instanceof ViewGroup) { + ViewGroup viewGroup = (ViewGroup) parent; + return viewGroup.getLayoutMode(); + } else { + int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion; + boolean preJellyBean = targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN; + return preJellyBean ? COMPONENT_BOUNDS : LAYOUT_BOUNDS; + } + + } + return mLayoutMode; + } + + /** + * Sets the basis of alignment during alignment of this view group. + * Valid values are either {@link #COMPONENT_BOUNDS} or {@link #LAYOUT_BOUNDS}. + * <p> + * The default is to query the property of the parent if this view group has a parent. + * If this ViewGroup is the root of the view hierarchy the default + * value is {@link #LAYOUT_BOUNDS} for target SDK's greater than JellyBean, + * {@link #LAYOUT_BOUNDS} otherwise. + * + * @return whether or not this view group should use the component or layout bounds during + * layout operations + * + * @see #getLayoutMode() + */ + public void setLayoutMode(int layoutMode) { + if (mLayoutMode != layoutMode) { + mLayoutMode = layoutMode; + requestLayout(); + } + } + + /** * Returns a new set of layout parameters based on the supplied attributes set. * * @param attrs the attributes to build the layout parameters from diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java index 60dd55c..1cb676f 100644 --- a/core/java/android/widget/GridLayout.java +++ b/core/java/android/widget/GridLayout.java @@ -20,6 +20,7 @@ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Insets; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; @@ -559,9 +560,9 @@ public class GridLayout extends ViewGroup { int flags = (gravity & mask) >> shift; switch (flags) { case (AXIS_SPECIFIED | AXIS_PULL_BEFORE): - return LEADING; + return horizontal ? LEFT : TOP; case (AXIS_SPECIFIED | AXIS_PULL_AFTER): - return TRAILING; + return horizontal ? RIGHT : BOTTOM; case (AXIS_SPECIFIED | AXIS_PULL_BEFORE | AXIS_PULL_AFTER): return FILL; case AXIS_SPECIFIED: @@ -1042,12 +1043,15 @@ public class GridLayout extends ViewGroup { int rightMargin = getMargin(c, true, false); int bottomMargin = getMargin(c, false, false); + int sumMarginsX = leftMargin + rightMargin; + int sumMarginsY = topMargin + bottomMargin; + // Alignment offsets: the location of the view relative to its alignment group. - int alignmentOffsetX = boundsX.getOffset(c, hAlign, leftMargin + pWidth + rightMargin); - int alignmentOffsetY = boundsY.getOffset(c, vAlign, topMargin + pHeight + bottomMargin); + int alignmentOffsetX = boundsX.getOffset(this, c, hAlign, pWidth + sumMarginsX, true); + int alignmentOffsetY = boundsY.getOffset(this, c, vAlign, pHeight + sumMarginsY, false); - int width = hAlign.getSizeInCell(c, pWidth, cellWidth - leftMargin - rightMargin); - int height = vAlign.getSizeInCell(c, pHeight, cellHeight - topMargin - bottomMargin); + int width = hAlign.getSizeInCell(c, pWidth, cellWidth - sumMarginsX); + int height = vAlign.getSizeInCell(c, pHeight, cellHeight - sumMarginsY); int dx = x1 + gravityOffsetX + alignmentOffsetX; @@ -1181,7 +1185,7 @@ public class GridLayout extends ViewGroup { View c = getChildAt(i); LayoutParams lp = getLayoutParams(c); Spec spec = horizontal ? lp.columnSpec : lp.rowSpec; - groupBounds.getValue(i).include(c, spec, GridLayout.this, this); + groupBounds.getValue(i).include(GridLayout.this, c, spec, this); } } @@ -2138,16 +2142,30 @@ public class GridLayout extends ViewGroup { return before + after; } - protected int getOffset(View c, Alignment alignment, int size) { - return before - alignment.getAlignmentValue(c, size); + private int getAlignmentValue(GridLayout gl, View c, int size, Alignment a, boolean horiz) { + boolean useLayoutBounds = gl.getLayoutMode() == LAYOUT_BOUNDS; + if (!useLayoutBounds) { + return a.getAlignmentValue(c, size); + } else { + Insets insets = c.getLayoutInsets(); + int leadingInset = horiz ? insets.left : insets.top; // RTL? + int trailingInset = horiz ? insets.right : insets.bottom; // RTL? + int totalInset = leadingInset + trailingInset; + return leadingInset + a.getAlignmentValue(c, size - totalInset); + } + } + + protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean horizontal) { + return before - getAlignmentValue(gl, c, size, a, horizontal); } - protected final void include(View c, Spec spec, GridLayout gridLayout, Axis axis) { + protected final void include(GridLayout gl, View c, Spec spec, Axis axis) { this.flexibility &= spec.getFlexibility(); - int size = gridLayout.getMeasurementIncludingMargin(c, axis.horizontal); - Alignment alignment = gridLayout.getAlignment(spec.alignment, axis.horizontal); + boolean horizontal = axis.horizontal; + int size = gl.getMeasurementIncludingMargin(c, horizontal); + Alignment alignment = gl.getAlignment(spec.alignment, horizontal); // todo test this works correctly when the returned value is UNDEFINED - int before = alignment.getAlignmentValue(c, size); + int before = getAlignmentValue(gl, c, size, alignment, horizontal); include(before, size - before); } @@ -2614,8 +2632,8 @@ public class GridLayout extends ViewGroup { } @Override - protected int getOffset(View c, Alignment alignment, int size) { - return max(0, super.getOffset(c, alignment, size)); + protected int getOffset(GridLayout gl, View c, Alignment a, int size, boolean hrz) { + return max(0, super.getOffset(gl, c, a, size, hrz)); } }; } |