The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 19 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 20 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 26 | import android.os.Build; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 27 | import android.util.ArrayMap; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 29 | import android.util.Pools.SynchronizedPool; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 30 | import android.util.SparseArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 31 | import android.view.Gravity; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewDebug; |
| 34 | import android.view.ViewGroup; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 35 | import android.view.ViewHierarchyEncoder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 36 | import android.view.accessibility.AccessibilityEvent; |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 37 | import android.view.inspector.InspectableProperty; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 38 | import android.widget.RemoteViews.RemoteView; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 39 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 40 | import com.android.internal.R; |
| 41 | |
| 42 | import java.util.ArrayDeque; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.Comparator; |
| 45 | import java.util.SortedSet; |
| 46 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * A Layout where the positions of the children can be described in relation to each other or to the |
Romain Guy | 520c420 | 2010-01-04 10:56:38 -0800 | [diff] [blame] | 50 | * parent. |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 51 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | * <p> |
| 53 | * Note that you cannot have a circular dependency between the size of the RelativeLayout and the |
| 54 | * position of its children. For example, you cannot have a RelativeLayout whose height is set to |
| 55 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT WRAP_CONTENT} and a child set to |
| 56 | * {@link #ALIGN_PARENT_BOTTOM}. |
| 57 | * </p> |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 58 | * |
Adam Powell | 2c8cc97 | 2012-12-07 18:04:51 -0800 | [diff] [blame] | 59 | * <p><strong>Note:</strong> In platform version 17 and lower, RelativeLayout was affected by |
| 60 | * a measurement bug that could cause child views to be measured with incorrect |
| 61 | * {@link android.view.View.MeasureSpec MeasureSpec} values. (See |
| 62 | * {@link android.view.View.MeasureSpec#makeMeasureSpec(int, int) MeasureSpec.makeMeasureSpec} |
| 63 | * for more details.) This was triggered when a RelativeLayout container was placed in |
| 64 | * a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view |
| 65 | * not equipped to properly measure with the MeasureSpec mode |
| 66 | * {@link android.view.View.MeasureSpec#UNSPECIFIED UNSPECIFIED} was placed in a RelativeLayout, |
| 67 | * this would silently work anyway as RelativeLayout would pass a very large |
| 68 | * {@link android.view.View.MeasureSpec#AT_MOST AT_MOST} MeasureSpec instead.</p> |
| 69 | * |
| 70 | * <p>This behavior has been preserved for apps that set <code>android:targetSdkVersion="17"</code> |
| 71 | * or older in their manifest's <code>uses-sdk</code> tag for compatibility. Apps targeting SDK |
| 72 | * version 18 or newer will receive the correct behavior</p> |
| 73 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 74 | * <p>See the <a href="{@docRoot}guide/topics/ui/layout/relative.html">Relative |
| 75 | * Layout</a> guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 76 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | * <p> |
| 78 | * Also see {@link android.widget.RelativeLayout.LayoutParams RelativeLayout.LayoutParams} for |
| 79 | * layout attributes |
| 80 | * </p> |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 81 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 83 | * @attr ref android.R.styleable#RelativeLayout_ignoreGravity |
| 84 | */ |
| 85 | @RemoteView |
| 86 | public class RelativeLayout extends ViewGroup { |
| 87 | public static final int TRUE = -1; |
| 88 | |
| 89 | /** |
| 90 | * Rule that aligns a child's right edge with another child's left edge. |
| 91 | */ |
| 92 | public static final int LEFT_OF = 0; |
| 93 | /** |
| 94 | * Rule that aligns a child's left edge with another child's right edge. |
| 95 | */ |
| 96 | public static final int RIGHT_OF = 1; |
| 97 | /** |
| 98 | * Rule that aligns a child's bottom edge with another child's top edge. |
| 99 | */ |
| 100 | public static final int ABOVE = 2; |
| 101 | /** |
| 102 | * Rule that aligns a child's top edge with another child's bottom edge. |
| 103 | */ |
| 104 | public static final int BELOW = 3; |
| 105 | |
| 106 | /** |
| 107 | * Rule that aligns a child's baseline with another child's baseline. |
| 108 | */ |
| 109 | public static final int ALIGN_BASELINE = 4; |
| 110 | /** |
| 111 | * Rule that aligns a child's left edge with another child's left edge. |
| 112 | */ |
| 113 | public static final int ALIGN_LEFT = 5; |
| 114 | /** |
| 115 | * Rule that aligns a child's top edge with another child's top edge. |
| 116 | */ |
| 117 | public static final int ALIGN_TOP = 6; |
| 118 | /** |
| 119 | * Rule that aligns a child's right edge with another child's right edge. |
| 120 | */ |
| 121 | public static final int ALIGN_RIGHT = 7; |
| 122 | /** |
| 123 | * Rule that aligns a child's bottom edge with another child's bottom edge. |
| 124 | */ |
| 125 | public static final int ALIGN_BOTTOM = 8; |
| 126 | |
| 127 | /** |
| 128 | * Rule that aligns the child's left edge with its RelativeLayout |
| 129 | * parent's left edge. |
| 130 | */ |
| 131 | public static final int ALIGN_PARENT_LEFT = 9; |
| 132 | /** |
| 133 | * Rule that aligns the child's top edge with its RelativeLayout |
| 134 | * parent's top edge. |
| 135 | */ |
| 136 | public static final int ALIGN_PARENT_TOP = 10; |
| 137 | /** |
| 138 | * Rule that aligns the child's right edge with its RelativeLayout |
| 139 | * parent's right edge. |
| 140 | */ |
| 141 | public static final int ALIGN_PARENT_RIGHT = 11; |
| 142 | /** |
| 143 | * Rule that aligns the child's bottom edge with its RelativeLayout |
| 144 | * parent's bottom edge. |
| 145 | */ |
| 146 | public static final int ALIGN_PARENT_BOTTOM = 12; |
| 147 | |
| 148 | /** |
| 149 | * Rule that centers the child with respect to the bounds of its |
| 150 | * RelativeLayout parent. |
| 151 | */ |
| 152 | public static final int CENTER_IN_PARENT = 13; |
| 153 | /** |
| 154 | * Rule that centers the child horizontally with respect to the |
| 155 | * bounds of its RelativeLayout parent. |
| 156 | */ |
| 157 | public static final int CENTER_HORIZONTAL = 14; |
| 158 | /** |
| 159 | * Rule that centers the child vertically with respect to the |
| 160 | * bounds of its RelativeLayout parent. |
| 161 | */ |
| 162 | public static final int CENTER_VERTICAL = 15; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Rule that aligns a child's end edge with another child's start edge. |
| 165 | */ |
| 166 | public static final int START_OF = 16; |
| 167 | /** |
| 168 | * Rule that aligns a child's start edge with another child's end edge. |
| 169 | */ |
| 170 | public static final int END_OF = 17; |
| 171 | /** |
| 172 | * Rule that aligns a child's start edge with another child's start edge. |
| 173 | */ |
| 174 | public static final int ALIGN_START = 18; |
| 175 | /** |
| 176 | * Rule that aligns a child's end edge with another child's end edge. |
| 177 | */ |
| 178 | public static final int ALIGN_END = 19; |
| 179 | /** |
| 180 | * Rule that aligns the child's start edge with its RelativeLayout |
| 181 | * parent's start edge. |
| 182 | */ |
| 183 | public static final int ALIGN_PARENT_START = 20; |
| 184 | /** |
| 185 | * Rule that aligns the child's end edge with its RelativeLayout |
| 186 | * parent's end edge. |
| 187 | */ |
| 188 | public static final int ALIGN_PARENT_END = 21; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 190 | private static final int VERB_COUNT = 22; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 192 | |
| 193 | private static final int[] RULES_VERTICAL = { |
| 194 | ABOVE, BELOW, ALIGN_BASELINE, ALIGN_TOP, ALIGN_BOTTOM |
| 195 | }; |
| 196 | |
| 197 | private static final int[] RULES_HORIZONTAL = { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 198 | LEFT_OF, RIGHT_OF, ALIGN_LEFT, ALIGN_RIGHT, START_OF, END_OF, ALIGN_START, ALIGN_END |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Used to indicate left/right/top/bottom should be inferred from constraints |
| 203 | */ |
| 204 | private static final int VALUE_NOT_SET = Integer.MIN_VALUE; |
| 205 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | private View mBaselineView = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | |
Clara Bayarri | 6036858 | 2018-11-22 15:13:52 +0000 | [diff] [blame] | 208 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 209 | private int mGravity = Gravity.START | Gravity.TOP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | private final Rect mContentBounds = new Rect(); |
| 211 | private final Rect mSelfBounds = new Rect(); |
| 212 | private int mIgnoreGravity; |
| 213 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 214 | private SortedSet<View> mTopToBottomLeftToRightSet = null; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 215 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 216 | private boolean mDirtyHierarchy; |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 217 | private View[] mSortedHorizontalChildren; |
| 218 | private View[] mSortedVerticalChildren; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 219 | private final DependencyGraph mGraph = new DependencyGraph(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 220 | |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 221 | // Compatibility hack. Old versions of the platform had problems |
| 222 | // with MeasureSpec value overflow and RelativeLayout was one source of them. |
| 223 | // Some apps came to rely on them. :( |
| 224 | private boolean mAllowBrokenMeasureSpecs = false; |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 225 | // Compatibility hack. Old versions of the platform would not take |
| 226 | // margins and padding into account when generating the height measure spec |
| 227 | // for children during the horizontal measure pass. |
| 228 | private boolean mMeasureVerticalWithPaddingMargin = false; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 229 | |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 230 | // A default width used for RTL measure pass |
Philip Milne | ca2e9e1 | 2013-04-22 12:44:29 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Value reduced so as not to interfere with View's measurement spec. flags. See: |
| 233 | * {@link View#MEASURED_SIZE_MASK}. |
| 234 | * {@link View#MEASURED_STATE_TOO_SMALL}. |
| 235 | **/ |
Philip Milne | 3c647d2 | 2013-04-23 14:31:23 -0700 | [diff] [blame] | 236 | private static final int DEFAULT_WIDTH = 0x00010000; |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | public RelativeLayout(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 239 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | public RelativeLayout(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 243 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 246 | public RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 247 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | public RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 251 | super(context, attrs, defStyleAttr, defStyleRes); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 252 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 253 | queryCompatibilityModes(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 256 | private void initFromAttributes( |
| 257 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 258 | final TypedArray a = context.obtainStyledAttributes( |
| 259 | attrs, R.styleable.RelativeLayout, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 260 | saveAttributeDataForStyleable(context, R.styleable.RelativeLayout, |
| 261 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | mIgnoreGravity = a.getResourceId(R.styleable.RelativeLayout_ignoreGravity, View.NO_ID); |
| 263 | mGravity = a.getInt(R.styleable.RelativeLayout_gravity, mGravity); |
| 264 | a.recycle(); |
| 265 | } |
| 266 | |
Romain Guy | 76d59a3 | 2013-04-08 10:51:35 -0700 | [diff] [blame] | 267 | private void queryCompatibilityModes(Context context) { |
| 268 | int version = context.getApplicationInfo().targetSdkVersion; |
| 269 | mAllowBrokenMeasureSpecs = version <= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 270 | mMeasureVerticalWithPaddingMargin = version >= Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 271 | } |
| 272 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 273 | @Override |
| 274 | public boolean shouldDelayChildPressedState() { |
| 275 | return false; |
| 276 | } |
| 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Defines which View is ignored when the gravity is applied. This setting has no |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 280 | * effect if the gravity is <code>Gravity.START | Gravity.TOP</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | * |
| 282 | * @param viewId The id of the View to be ignored by gravity, or 0 if no View |
| 283 | * should be ignored. |
| 284 | * |
| 285 | * @see #setGravity(int) |
| 286 | * |
| 287 | * @attr ref android.R.styleable#RelativeLayout_ignoreGravity |
| 288 | */ |
| 289 | @android.view.RemotableViewMethod |
| 290 | public void setIgnoreGravity(int viewId) { |
| 291 | mIgnoreGravity = viewId; |
| 292 | } |
| 293 | |
| 294 | /** |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 295 | * Get the id of the View to be ignored by gravity |
| 296 | * |
| 297 | * @attr ref android.R.styleable#RelativeLayout_ignoreGravity |
| 298 | */ |
| 299 | @InspectableProperty |
| 300 | public int getIgnoreGravity() { |
| 301 | return mIgnoreGravity; |
| 302 | } |
| 303 | |
| 304 | /** |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 305 | * Describes how the child views are positioned. |
| 306 | * |
| 307 | * @return the gravity. |
| 308 | * |
| 309 | * @see #setGravity(int) |
| 310 | * @see android.view.Gravity |
| 311 | * |
| 312 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 313 | */ |
Ashley Rose | 55f9f92 | 2019-01-28 19:29:36 -0500 | [diff] [blame] | 314 | @InspectableProperty(valueType = InspectableProperty.ValueType.GRAVITY) |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 315 | public int getGravity() { |
| 316 | return mGravity; |
| 317 | } |
| 318 | |
| 319 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | * Describes how the child views are positioned. Defaults to |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 321 | * <code>Gravity.START | Gravity.TOP</code>. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | * |
Adam Powell | 1fec24e | 2011-09-15 14:19:30 -0700 | [diff] [blame] | 323 | * <p>Note that since RelativeLayout considers the positioning of each child |
| 324 | * relative to one another to be significant, setting gravity will affect |
| 325 | * the positioning of all children as a single unit within the parent. |
| 326 | * This happens after children have been relatively positioned.</p> |
| 327 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | * @param gravity See {@link android.view.Gravity} |
| 329 | * |
| 330 | * @see #setHorizontalGravity(int) |
| 331 | * @see #setVerticalGravity(int) |
| 332 | * |
| 333 | * @attr ref android.R.styleable#RelativeLayout_gravity |
| 334 | */ |
| 335 | @android.view.RemotableViewMethod |
| 336 | public void setGravity(int gravity) { |
| 337 | if (mGravity != gravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 338 | if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 339 | gravity |= Gravity.START; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { |
| 343 | gravity |= Gravity.TOP; |
| 344 | } |
| 345 | |
| 346 | mGravity = gravity; |
| 347 | requestLayout(); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | @android.view.RemotableViewMethod |
| 352 | public void setHorizontalGravity(int horizontalGravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 353 | final int gravity = horizontalGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 354 | if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) { |
| 355 | mGravity = (mGravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | requestLayout(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | @android.view.RemotableViewMethod |
| 361 | public void setVerticalGravity(int verticalGravity) { |
| 362 | final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 363 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) { |
| 364 | mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity; |
| 365 | requestLayout(); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public int getBaseline() { |
| 371 | return mBaselineView != null ? mBaselineView.getBaseline() : super.getBaseline(); |
| 372 | } |
| 373 | |
| 374 | @Override |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 375 | public void requestLayout() { |
| 376 | super.requestLayout(); |
| 377 | mDirtyHierarchy = true; |
| 378 | } |
| 379 | |
| 380 | private void sortChildren() { |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 381 | final int count = getChildCount(); |
| 382 | if (mSortedVerticalChildren == null || mSortedVerticalChildren.length != count) { |
| 383 | mSortedVerticalChildren = new View[count]; |
| 384 | } |
| 385 | |
| 386 | if (mSortedHorizontalChildren == null || mSortedHorizontalChildren.length != count) { |
| 387 | mSortedHorizontalChildren = new View[count]; |
| 388 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 389 | |
| 390 | final DependencyGraph graph = mGraph; |
| 391 | graph.clear(); |
| 392 | |
| 393 | for (int i = 0; i < count; i++) { |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 394 | graph.add(getChildAt(i)); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 397 | graph.getSortedViews(mSortedVerticalChildren, RULES_VERTICAL); |
| 398 | graph.getSortedViews(mSortedHorizontalChildren, RULES_HORIZONTAL); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 403 | if (mDirtyHierarchy) { |
| 404 | mDirtyHierarchy = false; |
| 405 | sortChildren(); |
| 406 | } |
| 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | int myWidth = -1; |
| 409 | int myHeight = -1; |
| 410 | |
| 411 | int width = 0; |
| 412 | int height = 0; |
| 413 | |
Adam Powell | 132a742 | 2012-09-30 12:39:56 -0700 | [diff] [blame] | 414 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 415 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 416 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 417 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | |
| 419 | // Record our dimensions if they are known; |
| 420 | if (widthMode != MeasureSpec.UNSPECIFIED) { |
| 421 | myWidth = widthSize; |
| 422 | } |
| 423 | |
| 424 | if (heightMode != MeasureSpec.UNSPECIFIED) { |
| 425 | myHeight = heightSize; |
| 426 | } |
| 427 | |
| 428 | if (widthMode == MeasureSpec.EXACTLY) { |
| 429 | width = myWidth; |
| 430 | } |
| 431 | |
| 432 | if (heightMode == MeasureSpec.EXACTLY) { |
| 433 | height = myHeight; |
| 434 | } |
| 435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | View ignore = null; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 437 | int gravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 438 | final boolean horizontalGravity = gravity != Gravity.START && gravity != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 440 | final boolean verticalGravity = gravity != Gravity.TOP && gravity != 0; |
| 441 | |
| 442 | int left = Integer.MAX_VALUE; |
| 443 | int top = Integer.MAX_VALUE; |
| 444 | int right = Integer.MIN_VALUE; |
| 445 | int bottom = Integer.MIN_VALUE; |
| 446 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 447 | boolean offsetHorizontalAxis = false; |
| 448 | boolean offsetVerticalAxis = false; |
| 449 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) { |
| 451 | ignore = findViewById(mIgnoreGravity); |
| 452 | } |
| 453 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 454 | final boolean isWrapContentWidth = widthMode != MeasureSpec.EXACTLY; |
| 455 | final boolean isWrapContentHeight = heightMode != MeasureSpec.EXACTLY; |
| 456 | |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 457 | // We need to know our size for doing the correct computation of children positioning in RTL |
| 458 | // mode but there is no practical way to get it instead of running the code below. |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 459 | // So, instead of running the code twice, we just set the width to a "default display width" |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 460 | // before the computation and then, as a last pass, we will update their real position with |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 461 | // an offset equals to "DEFAULT_WIDTH - width". |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 462 | final int layoutDirection = getLayoutDirection(); |
| 463 | if (isLayoutRtl() && myWidth == -1) { |
Fabrice Di Meglio | d5ffc79 | 2013-02-19 15:59:21 -0800 | [diff] [blame] | 464 | myWidth = DEFAULT_WIDTH; |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 465 | } |
| 466 | |
Romain Guy | e24ef60 | 2009-06-25 13:01:55 -0700 | [diff] [blame] | 467 | View[] views = mSortedHorizontalChildren; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 468 | int count = views.length; |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 469 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 470 | for (int i = 0; i < count; i++) { |
| 471 | View child = views[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | if (child.getVisibility() != GONE) { |
| 473 | LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 474 | int[] rules = params.getRules(layoutDirection); |
Romain Guy | 9560703 | 2009-06-24 14:37:03 -0700 | [diff] [blame] | 475 | |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 476 | applyHorizontalSizeRules(params, myWidth, rules); |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 477 | measureChildHorizontal(child, params, myWidth, myHeight); |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 478 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 479 | if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) { |
| 480 | offsetHorizontalAxis = true; |
| 481 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Romain Guy | e24ef60 | 2009-06-25 13:01:55 -0700 | [diff] [blame] | 485 | views = mSortedVerticalChildren; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 486 | count = views.length; |
Fabrice Di Meglio | 2bd961a | 2013-08-27 19:46:43 -0700 | [diff] [blame] | 487 | final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 488 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 489 | for (int i = 0; i < count; i++) { |
Alan Viverette | e1a6571 | 2015-01-14 12:30:04 -0800 | [diff] [blame] | 490 | final View child = views[i]; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 491 | if (child.getVisibility() != GONE) { |
Alan Viverette | e1a6571 | 2015-01-14 12:30:04 -0800 | [diff] [blame] | 492 | final LayoutParams params = (LayoutParams) child.getLayoutParams(); |
| 493 | |
| 494 | applyVerticalSizeRules(params, myHeight, child.getBaseline()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | measureChild(child, params, myWidth, myHeight); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 496 | if (positionChildVertical(child, params, myHeight, isWrapContentHeight)) { |
| 497 | offsetVerticalAxis = true; |
| 498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 500 | if (isWrapContentWidth) { |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 501 | if (isLayoutRtl()) { |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 502 | if (targetSdkVersion < Build.VERSION_CODES.KITKAT) { |
Fabrice Di Meglio | 2bd961a | 2013-08-27 19:46:43 -0700 | [diff] [blame] | 503 | width = Math.max(width, myWidth - params.mLeft); |
| 504 | } else { |
Roozbeh Pournader | 4d4596a | 2017-07-13 18:35:09 -0700 | [diff] [blame] | 505 | width = Math.max(width, myWidth - params.mLeft + params.leftMargin); |
Fabrice Di Meglio | 2bd961a | 2013-08-27 19:46:43 -0700 | [diff] [blame] | 506 | } |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 507 | } else { |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 508 | if (targetSdkVersion < Build.VERSION_CODES.KITKAT) { |
Fabrice Di Meglio | 2bd961a | 2013-08-27 19:46:43 -0700 | [diff] [blame] | 509 | width = Math.max(width, params.mRight); |
| 510 | } else { |
| 511 | width = Math.max(width, params.mRight + params.rightMargin); |
| 512 | } |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 514 | } |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 515 | |
| 516 | if (isWrapContentHeight) { |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 517 | if (targetSdkVersion < Build.VERSION_CODES.KITKAT) { |
Fabrice Di Meglio | 2bd961a | 2013-08-27 19:46:43 -0700 | [diff] [blame] | 518 | height = Math.max(height, params.mBottom); |
| 519 | } else { |
| 520 | height = Math.max(height, params.mBottom + params.bottomMargin); |
| 521 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (child != ignore || verticalGravity) { |
| 525 | left = Math.min(left, params.mLeft - params.leftMargin); |
| 526 | top = Math.min(top, params.mTop - params.topMargin); |
| 527 | } |
| 528 | |
| 529 | if (child != ignore || horizontalGravity) { |
| 530 | right = Math.max(right, params.mRight + params.rightMargin); |
| 531 | bottom = Math.max(bottom, params.mBottom + params.bottomMargin); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
Alan Viverette | 0e14a85 | 2015-03-09 18:01:19 -0700 | [diff] [blame] | 536 | // Use the top-start-most laid out view as the baseline. RTL offsets are |
| 537 | // applied later, so we can use the left-most edge as the starting edge. |
| 538 | View baselineView = null; |
| 539 | LayoutParams baselineParams = null; |
| 540 | for (int i = 0; i < count; i++) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 541 | final View child = views[i]; |
Alan Viverette | 0e14a85 | 2015-03-09 18:01:19 -0700 | [diff] [blame] | 542 | if (child.getVisibility() != GONE) { |
| 543 | final LayoutParams childParams = (LayoutParams) child.getLayoutParams(); |
| 544 | if (baselineView == null || baselineParams == null |
| 545 | || compareLayoutPosition(childParams, baselineParams) < 0) { |
| 546 | baselineView = child; |
| 547 | baselineParams = childParams; |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | mBaselineView = baselineView; |
| 552 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 553 | if (isWrapContentWidth) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 554 | // Width already has left padding in it since it was calculated by looking at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | // the right of each child view |
| 556 | width += mPaddingRight; |
| 557 | |
Adam Powell | 758d5a4 | 2013-09-13 09:22:30 -0700 | [diff] [blame] | 558 | if (mLayoutParams != null && mLayoutParams.width >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | width = Math.max(width, mLayoutParams.width); |
| 560 | } |
| 561 | |
| 562 | width = Math.max(width, getSuggestedMinimumWidth()); |
| 563 | width = resolveSize(width, widthMeasureSpec); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 564 | |
| 565 | if (offsetHorizontalAxis) { |
Romain Guy | d10a576 | 2009-07-28 11:18:14 -0700 | [diff] [blame] | 566 | for (int i = 0; i < count; i++) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 567 | final View child = views[i]; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 568 | if (child.getVisibility() != GONE) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 569 | final LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 570 | final int[] rules = params.getRules(layoutDirection); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 571 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) { |
| 572 | centerHorizontal(child, params, width); |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 573 | } else if (rules[ALIGN_PARENT_RIGHT] != 0) { |
| 574 | final int childWidth = child.getMeasuredWidth(); |
| 575 | params.mLeft = width - mPaddingRight - childWidth; |
| 576 | params.mRight = params.mLeft + childWidth; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | } |
| 580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 582 | |
| 583 | if (isWrapContentHeight) { |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 584 | // Height already has top padding in it since it was calculated by looking at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | // the bottom of each child view |
| 586 | height += mPaddingBottom; |
| 587 | |
Adam Powell | 758d5a4 | 2013-09-13 09:22:30 -0700 | [diff] [blame] | 588 | if (mLayoutParams != null && mLayoutParams.height >= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 589 | height = Math.max(height, mLayoutParams.height); |
| 590 | } |
| 591 | |
| 592 | height = Math.max(height, getSuggestedMinimumHeight()); |
| 593 | height = resolveSize(height, heightMeasureSpec); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 594 | |
| 595 | if (offsetVerticalAxis) { |
| 596 | for (int i = 0; i < count; i++) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 597 | final View child = views[i]; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 598 | if (child.getVisibility() != GONE) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 599 | final LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 600 | final int[] rules = params.getRules(layoutDirection); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 601 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) { |
| 602 | centerVertical(child, params, height); |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 603 | } else if (rules[ALIGN_PARENT_BOTTOM] != 0) { |
| 604 | final int childHeight = child.getMeasuredHeight(); |
| 605 | params.mTop = height - mPaddingBottom - childHeight; |
| 606 | params.mBottom = params.mTop + childHeight; |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if (horizontalGravity || verticalGravity) { |
| 614 | final Rect selfBounds = mSelfBounds; |
| 615 | selfBounds.set(mPaddingLeft, mPaddingTop, width - mPaddingRight, |
| 616 | height - mPaddingBottom); |
| 617 | |
| 618 | final Rect contentBounds = mContentBounds; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 619 | Gravity.apply(mGravity, right - left, bottom - top, selfBounds, contentBounds, |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 620 | layoutDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | |
| 622 | final int horizontalOffset = contentBounds.left - left; |
| 623 | final int verticalOffset = contentBounds.top - top; |
| 624 | if (horizontalOffset != 0 || verticalOffset != 0) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 625 | for (int i = 0; i < count; i++) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 626 | final View child = views[i]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | if (child.getVisibility() != GONE && child != ignore) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 628 | final LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Romain Guy | d10a576 | 2009-07-28 11:18:14 -0700 | [diff] [blame] | 629 | if (horizontalGravity) { |
| 630 | params.mLeft += horizontalOffset; |
| 631 | params.mRight += horizontalOffset; |
| 632 | } |
| 633 | if (verticalGravity) { |
| 634 | params.mTop += verticalOffset; |
| 635 | params.mBottom += verticalOffset; |
| 636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 642 | if (isLayoutRtl()) { |
| 643 | final int offsetWidth = myWidth - width; |
| 644 | for (int i = 0; i < count; i++) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 645 | final View child = views[i]; |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 646 | if (child.getVisibility() != GONE) { |
Alan Viverette | 9554f9f | 2015-06-22 22:37:58 +0000 | [diff] [blame] | 647 | final LayoutParams params = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 648 | params.mLeft -= offsetWidth; |
| 649 | params.mRight -= offsetWidth; |
| 650 | } |
| 651 | } |
Fabrice Di Meglio | 306fe5c | 2013-01-23 18:46:25 -0800 | [diff] [blame] | 652 | } |
| 653 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | setMeasuredDimension(width, height); |
| 655 | } |
| 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | /** |
Alan Viverette | 0e14a85 | 2015-03-09 18:01:19 -0700 | [diff] [blame] | 658 | * @return a negative number if the top of {@code p1} is above the top of |
| 659 | * {@code p2} or if they have identical top values and the left of |
| 660 | * {@code p1} is to the left of {@code p2}, or a positive number |
| 661 | * otherwise |
| 662 | */ |
| 663 | private int compareLayoutPosition(LayoutParams p1, LayoutParams p2) { |
| 664 | final int topDiff = p1.mTop - p2.mTop; |
| 665 | if (topDiff != 0) { |
| 666 | return topDiff; |
| 667 | } |
| 668 | return p1.mLeft - p2.mLeft; |
| 669 | } |
| 670 | |
| 671 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | * Measure a child. The child should have left, top, right and bottom information |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 673 | * stored in its LayoutParams. If any of these values is VALUE_NOT_SET it means |
| 674 | * that the view can extend up to the corresponding edge. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | * |
| 676 | * @param child Child to measure |
| 677 | * @param params LayoutParams associated with child |
| 678 | * @param myWidth Width of the the RelativeLayout |
| 679 | * @param myHeight Height of the RelativeLayout |
| 680 | */ |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 681 | private void measureChild(View child, LayoutParams params, int myWidth, int myHeight) { |
Romain Guy | e8fb03c | 2013-04-25 11:40:45 -0700 | [diff] [blame] | 682 | int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft, |
| 683 | params.mRight, params.width, |
| 684 | params.leftMargin, params.rightMargin, |
| 685 | mPaddingLeft, mPaddingRight, |
| 686 | myWidth); |
Romain Guy | 1b7c791 | 2013-04-25 14:55:28 -0700 | [diff] [blame] | 687 | int childHeightMeasureSpec = getChildMeasureSpec(params.mTop, |
| 688 | params.mBottom, params.height, |
| 689 | params.topMargin, params.bottomMargin, |
| 690 | mPaddingTop, mPaddingBottom, |
| 691 | myHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 693 | } |
| 694 | |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 695 | private void measureChildHorizontal( |
| 696 | View child, LayoutParams params, int myWidth, int myHeight) { |
| 697 | final int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft, params.mRight, |
| 698 | params.width, params.leftMargin, params.rightMargin, mPaddingLeft, mPaddingRight, |
Romain Guy | 9560703 | 2009-06-24 14:37:03 -0700 | [diff] [blame] | 699 | myWidth); |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 700 | |
| 701 | final int childHeightMeasureSpec; |
Adam Powell | 7da4b73 | 2012-12-07 15:28:33 -0800 | [diff] [blame] | 702 | if (myHeight < 0 && !mAllowBrokenMeasureSpecs) { |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 703 | if (params.height >= 0) { |
| 704 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 705 | params.height, MeasureSpec.EXACTLY); |
| 706 | } else { |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 707 | // Negative values in a mySize/myWidth/myWidth value in |
| 708 | // RelativeLayout measurement is code for, "we got an |
| 709 | // unspecified mode in the RelativeLayout's measure spec." |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 710 | // Carry it forward. |
| 711 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 712 | } |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 713 | } else { |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 714 | final int maxHeight; |
| 715 | if (mMeasureVerticalWithPaddingMargin) { |
| 716 | maxHeight = Math.max(0, myHeight - mPaddingTop - mPaddingBottom |
| 717 | - params.topMargin - params.bottomMargin); |
| 718 | } else { |
| 719 | maxHeight = Math.max(0, myHeight); |
| 720 | } |
| 721 | |
| 722 | final int heightMode; |
Alan Viverette | 130ce74 | 2015-08-12 13:04:16 -0400 | [diff] [blame] | 723 | if (params.height == LayoutParams.MATCH_PARENT) { |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 724 | heightMode = MeasureSpec.EXACTLY; |
| 725 | } else { |
| 726 | heightMode = MeasureSpec.AT_MOST; |
| 727 | } |
| 728 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, heightMode); |
Romain Guy | f782e60 | 2009-06-25 15:26:49 -0700 | [diff] [blame] | 729 | } |
Alan Viverette | 7a40d21 | 2015-07-29 15:55:20 -0400 | [diff] [blame] | 730 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 731 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 732 | } |
| 733 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 734 | /** |
| 735 | * Get a measure spec that accounts for all of the constraints on this view. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 736 | * This includes size constraints imposed by the RelativeLayout as well as |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | * the View's desired dimension. |
| 738 | * |
| 739 | * @param childStart The left or top field of the child's layout params |
| 740 | * @param childEnd The right or bottom field of the child's layout params |
| 741 | * @param childSize The child's desired size (the width or height field of |
| 742 | * the child's layout params) |
| 743 | * @param startMargin The left or top margin |
| 744 | * @param endMargin The right or bottom margin |
| 745 | * @param startPadding mPaddingLeft or mPaddingTop |
| 746 | * @param endPadding mPaddingRight or mPaddingBottom |
| 747 | * @param mySize The width or height of this view (the RelativeLayout) |
| 748 | * @return MeasureSpec for the child |
| 749 | */ |
| 750 | private int getChildMeasureSpec(int childStart, int childEnd, |
| 751 | int childSize, int startMargin, int endMargin, int startPadding, |
| 752 | int endPadding, int mySize) { |
| 753 | int childSpecMode = 0; |
| 754 | int childSpecSize = 0; |
| 755 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 756 | // Negative values in a mySize value in RelativeLayout |
Alan Viverette | 517a004 | 2013-12-04 17:08:44 -0800 | [diff] [blame] | 757 | // measurement is code for, "we got an unspecified mode in the |
| 758 | // RelativeLayout's measure spec." |
Alan Viverette | 5b39ec8 | 2015-07-22 15:37:14 -0400 | [diff] [blame] | 759 | final boolean isUnspecified = mySize < 0; |
| 760 | if (isUnspecified && !mAllowBrokenMeasureSpecs) { |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 761 | if (childStart != VALUE_NOT_SET && childEnd != VALUE_NOT_SET) { |
Alan Viverette | 517a004 | 2013-12-04 17:08:44 -0800 | [diff] [blame] | 762 | // Constraints fixed both edges, so child has an exact size. |
| 763 | childSpecSize = Math.max(0, childEnd - childStart); |
| 764 | childSpecMode = MeasureSpec.EXACTLY; |
Alan Viverette | d5694f0 | 2014-02-12 15:49:18 -0800 | [diff] [blame] | 765 | } else if (childSize >= 0) { |
| 766 | // The child specified an exact size. |
| 767 | childSpecSize = childSize; |
| 768 | childSpecMode = MeasureSpec.EXACTLY; |
Alan Viverette | 517a004 | 2013-12-04 17:08:44 -0800 | [diff] [blame] | 769 | } else { |
| 770 | // Allow the child to be whatever size it wants. |
| 771 | childSpecSize = 0; |
| 772 | childSpecMode = MeasureSpec.UNSPECIFIED; |
| 773 | } |
| 774 | |
| 775 | return MeasureSpec.makeMeasureSpec(childSpecSize, childSpecMode); |
| 776 | } |
| 777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | // Figure out start and end bounds. |
| 779 | int tempStart = childStart; |
| 780 | int tempEnd = childEnd; |
| 781 | |
| 782 | // If the view did not express a layout constraint for an edge, use |
| 783 | // view's margins and our padding |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 784 | if (tempStart == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | tempStart = startPadding + startMargin; |
| 786 | } |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 787 | if (tempEnd == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | tempEnd = mySize - endPadding - endMargin; |
| 789 | } |
| 790 | |
| 791 | // Figure out maximum size available to this view |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 792 | final int maxAvailable = tempEnd - tempStart; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 793 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 794 | if (childStart != VALUE_NOT_SET && childEnd != VALUE_NOT_SET) { |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 795 | // Constraints fixed both edges, so child must be an exact size. |
Alan Viverette | 5b39ec8 | 2015-07-22 15:37:14 -0400 | [diff] [blame] | 796 | childSpecMode = isUnspecified ? MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY; |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 797 | childSpecSize = Math.max(0, maxAvailable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | } else { |
| 799 | if (childSize >= 0) { |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 800 | // Child wanted an exact size. Give as much as possible. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | childSpecMode = MeasureSpec.EXACTLY; |
| 802 | |
| 803 | if (maxAvailable >= 0) { |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 804 | // We have a maximum size in this dimension. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | childSpecSize = Math.min(maxAvailable, childSize); |
| 806 | } else { |
| 807 | // We can grow in this dimension. |
| 808 | childSpecSize = childSize; |
| 809 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 810 | } else if (childSize == LayoutParams.MATCH_PARENT) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 811 | // Child wanted to be as big as possible. Give all available |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 812 | // space. |
Alan Viverette | 5b39ec8 | 2015-07-22 15:37:14 -0400 | [diff] [blame] | 813 | childSpecMode = isUnspecified ? MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY; |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 814 | childSpecSize = Math.max(0, maxAvailable); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 815 | } else if (childSize == LayoutParams.WRAP_CONTENT) { |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 816 | // Child wants to wrap content. Use AT_MOST to communicate |
| 817 | // available space if we know our max size. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | if (maxAvailable >= 0) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 819 | // We have a maximum size in this dimension. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | childSpecMode = MeasureSpec.AT_MOST; |
| 821 | childSpecSize = maxAvailable; |
| 822 | } else { |
| 823 | // We can grow in this dimension. Child can be as big as it |
Alan Viverette | 39310d3 | 2015-06-24 17:03:48 -0700 | [diff] [blame] | 824 | // wants. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | childSpecMode = MeasureSpec.UNSPECIFIED; |
| 826 | childSpecSize = 0; |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | return MeasureSpec.makeMeasureSpec(childSpecSize, childSpecMode); |
| 832 | } |
| 833 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 834 | private boolean positionChildHorizontal(View child, LayoutParams params, int myWidth, |
| 835 | boolean wrapContent) { |
| 836 | |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 837 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 838 | int[] rules = params.getRules(layoutDirection); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 839 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 840 | if (params.mLeft == VALUE_NOT_SET && params.mRight != VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | // Right is fixed, but left varies |
| 842 | params.mLeft = params.mRight - child.getMeasuredWidth(); |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 843 | } else if (params.mLeft != VALUE_NOT_SET && params.mRight == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | // Left is fixed, but right varies |
| 845 | params.mRight = params.mLeft + child.getMeasuredWidth(); |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 846 | } else if (params.mLeft == VALUE_NOT_SET && params.mRight == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 847 | // Both left and right vary |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 848 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) { |
| 849 | if (!wrapContent) { |
| 850 | centerHorizontal(child, params, myWidth); |
| 851 | } else { |
Chet Haase | bcd3520 | 2017-06-28 17:06:50 -0700 | [diff] [blame] | 852 | positionAtEdge(child, params, myWidth); |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 853 | } |
| 854 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | } else { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 856 | // This is the default case. For RTL we start from the right and for LTR we start |
| 857 | // from the left. This will give LEFT/TOP for LTR and RIGHT/TOP for RTL. |
Chet Haase | bcd3520 | 2017-06-28 17:06:50 -0700 | [diff] [blame] | 858 | positionAtEdge(child, params, myWidth); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
| 860 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 861 | return rules[ALIGN_PARENT_END] != 0; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Chet Haase | bcd3520 | 2017-06-28 17:06:50 -0700 | [diff] [blame] | 864 | private void positionAtEdge(View child, LayoutParams params, int myWidth) { |
| 865 | if (isLayoutRtl()) { |
| 866 | params.mRight = myWidth - mPaddingRight - params.rightMargin; |
| 867 | params.mLeft = params.mRight - child.getMeasuredWidth(); |
| 868 | } else { |
| 869 | params.mLeft = mPaddingLeft + params.leftMargin; |
| 870 | params.mRight = params.mLeft + child.getMeasuredWidth(); |
| 871 | } |
| 872 | } |
| 873 | |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 874 | private boolean positionChildVertical(View child, LayoutParams params, int myHeight, |
| 875 | boolean wrapContent) { |
| 876 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 877 | int[] rules = params.getRules(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 879 | if (params.mTop == VALUE_NOT_SET && params.mBottom != VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | // Bottom is fixed, but top varies |
| 881 | params.mTop = params.mBottom - child.getMeasuredHeight(); |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 882 | } else if (params.mTop != VALUE_NOT_SET && params.mBottom == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | // Top is fixed, but bottom varies |
| 884 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 885 | } else if (params.mTop == VALUE_NOT_SET && params.mBottom == VALUE_NOT_SET) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | // Both top and bottom vary |
Romain Guy | f7dabb0 | 2009-06-25 14:47:14 -0700 | [diff] [blame] | 887 | if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) { |
| 888 | if (!wrapContent) { |
| 889 | centerVertical(child, params, myHeight); |
| 890 | } else { |
| 891 | params.mTop = mPaddingTop + params.topMargin; |
| 892 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
| 893 | } |
| 894 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } else { |
| 896 | params.mTop = mPaddingTop + params.topMargin; |
| 897 | params.mBottom = params.mTop + child.getMeasuredHeight(); |
| 898 | } |
| 899 | } |
Romain Guy | 42460ac | 2010-01-11 16:46:33 -0800 | [diff] [blame] | 900 | return rules[ALIGN_PARENT_BOTTOM] != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Fabrice Di Meglio | 5472613 | 2013-01-18 18:36:45 -0800 | [diff] [blame] | 903 | private void applyHorizontalSizeRules(LayoutParams childParams, int myWidth, int[] rules) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | RelativeLayout.LayoutParams anchorParams; |
| 905 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 906 | // VALUE_NOT_SET indicates a "soft requirement" in that direction. For example: |
| 907 | // left=10, right=VALUE_NOT_SET means the view must start at 10, but can go as far as it |
| 908 | // wants to the right |
| 909 | // left=VALUE_NOT_SET, right=10 means the view must end at 10, but can go as far as it |
| 910 | // wants to the left |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | // left=10, right=20 means the left and right ends are both fixed |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 912 | childParams.mLeft = VALUE_NOT_SET; |
| 913 | childParams.mRight = VALUE_NOT_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | |
| 915 | anchorParams = getRelatedViewParams(rules, LEFT_OF); |
| 916 | if (anchorParams != null) { |
| 917 | childParams.mRight = anchorParams.mLeft - (anchorParams.leftMargin + |
| 918 | childParams.rightMargin); |
| 919 | } else if (childParams.alignWithParent && rules[LEFT_OF] != 0) { |
| 920 | if (myWidth >= 0) { |
| 921 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | anchorParams = getRelatedViewParams(rules, RIGHT_OF); |
| 926 | if (anchorParams != null) { |
| 927 | childParams.mLeft = anchorParams.mRight + (anchorParams.rightMargin + |
| 928 | childParams.leftMargin); |
| 929 | } else if (childParams.alignWithParent && rules[RIGHT_OF] != 0) { |
| 930 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 931 | } |
| 932 | |
| 933 | anchorParams = getRelatedViewParams(rules, ALIGN_LEFT); |
| 934 | if (anchorParams != null) { |
| 935 | childParams.mLeft = anchorParams.mLeft + childParams.leftMargin; |
| 936 | } else if (childParams.alignWithParent && rules[ALIGN_LEFT] != 0) { |
| 937 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 938 | } |
| 939 | |
| 940 | anchorParams = getRelatedViewParams(rules, ALIGN_RIGHT); |
| 941 | if (anchorParams != null) { |
| 942 | childParams.mRight = anchorParams.mRight - childParams.rightMargin; |
| 943 | } else if (childParams.alignWithParent && rules[ALIGN_RIGHT] != 0) { |
| 944 | if (myWidth >= 0) { |
| 945 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
Romain Guy | 1b7c791 | 2013-04-25 14:55:28 -0700 | [diff] [blame] | 949 | if (0 != rules[ALIGN_PARENT_LEFT]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | childParams.mLeft = mPaddingLeft + childParams.leftMargin; |
| 951 | } |
| 952 | |
Romain Guy | 1b7c791 | 2013-04-25 14:55:28 -0700 | [diff] [blame] | 953 | if (0 != rules[ALIGN_PARENT_RIGHT]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | if (myWidth >= 0) { |
| 955 | childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } |
| 957 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Alan Viverette | e1a6571 | 2015-01-14 12:30:04 -0800 | [diff] [blame] | 960 | private void applyVerticalSizeRules(LayoutParams childParams, int myHeight, int myBaseline) { |
| 961 | final int[] rules = childParams.getRules(); |
| 962 | |
| 963 | // Baseline alignment overrides any explicitly specified top or bottom. |
| 964 | int baselineOffset = getRelatedViewBaselineOffset(rules); |
| 965 | if (baselineOffset != -1) { |
| 966 | if (myBaseline != -1) { |
| 967 | baselineOffset -= myBaseline; |
| 968 | } |
| 969 | childParams.mTop = baselineOffset; |
| 970 | childParams.mBottom = VALUE_NOT_SET; |
| 971 | return; |
| 972 | } |
| 973 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 974 | RelativeLayout.LayoutParams anchorParams; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | |
Chet Haase | 28308e8 | 2014-07-22 16:38:16 -0700 | [diff] [blame] | 976 | childParams.mTop = VALUE_NOT_SET; |
| 977 | childParams.mBottom = VALUE_NOT_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | |
| 979 | anchorParams = getRelatedViewParams(rules, ABOVE); |
| 980 | if (anchorParams != null) { |
| 981 | childParams.mBottom = anchorParams.mTop - (anchorParams.topMargin + |
| 982 | childParams.bottomMargin); |
| 983 | } else if (childParams.alignWithParent && rules[ABOVE] != 0) { |
| 984 | if (myHeight >= 0) { |
| 985 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | anchorParams = getRelatedViewParams(rules, BELOW); |
| 990 | if (anchorParams != null) { |
| 991 | childParams.mTop = anchorParams.mBottom + (anchorParams.bottomMargin + |
| 992 | childParams.topMargin); |
| 993 | } else if (childParams.alignWithParent && rules[BELOW] != 0) { |
| 994 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 995 | } |
| 996 | |
| 997 | anchorParams = getRelatedViewParams(rules, ALIGN_TOP); |
| 998 | if (anchorParams != null) { |
| 999 | childParams.mTop = anchorParams.mTop + childParams.topMargin; |
| 1000 | } else if (childParams.alignWithParent && rules[ALIGN_TOP] != 0) { |
| 1001 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 1002 | } |
| 1003 | |
| 1004 | anchorParams = getRelatedViewParams(rules, ALIGN_BOTTOM); |
| 1005 | if (anchorParams != null) { |
| 1006 | childParams.mBottom = anchorParams.mBottom - childParams.bottomMargin; |
| 1007 | } else if (childParams.alignWithParent && rules[ALIGN_BOTTOM] != 0) { |
| 1008 | if (myHeight >= 0) { |
| 1009 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | |
Romain Guy | 1b7c791 | 2013-04-25 14:55:28 -0700 | [diff] [blame] | 1013 | if (0 != rules[ALIGN_PARENT_TOP]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | childParams.mTop = mPaddingTop + childParams.topMargin; |
| 1015 | } |
| 1016 | |
Romain Guy | 1b7c791 | 2013-04-25 14:55:28 -0700 | [diff] [blame] | 1017 | if (0 != rules[ALIGN_PARENT_BOTTOM]) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | if (myHeight >= 0) { |
| 1019 | childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | private View getRelatedView(int[] rules, int relation) { |
| 1025 | int id = rules[relation]; |
| 1026 | if (id != 0) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1027 | DependencyGraph.Node node = mGraph.mKeyNodes.get(id); |
Romain Guy | a0fd1d7 | 2009-06-24 14:25:43 -0700 | [diff] [blame] | 1028 | if (node == null) return null; |
| 1029 | View v = node.view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | |
| 1031 | // Find the first non-GONE view up the chain |
| 1032 | while (v.getVisibility() == View.GONE) { |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 1033 | rules = ((LayoutParams) v.getLayoutParams()).getRules(v.getLayoutDirection()); |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1034 | node = mGraph.mKeyNodes.get((rules[relation])); |
Yura | a354c4f | 2017-01-24 19:41:41 +0000 | [diff] [blame] | 1035 | // ignore self dependency. for more info look in git commit: da3003 |
| 1036 | if (node == null || v == node.view) return null; |
Romain Guy | a0fd1d7 | 2009-06-24 14:25:43 -0700 | [diff] [blame] | 1037 | v = node.view; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | return v; |
| 1041 | } |
| 1042 | |
| 1043 | return null; |
| 1044 | } |
| 1045 | |
| 1046 | private LayoutParams getRelatedViewParams(int[] rules, int relation) { |
| 1047 | View v = getRelatedView(rules, relation); |
| 1048 | if (v != null) { |
| 1049 | ViewGroup.LayoutParams params = v.getLayoutParams(); |
| 1050 | if (params instanceof LayoutParams) { |
| 1051 | return (LayoutParams) v.getLayoutParams(); |
| 1052 | } |
| 1053 | } |
| 1054 | return null; |
| 1055 | } |
| 1056 | |
Alan Viverette | e1a6571 | 2015-01-14 12:30:04 -0800 | [diff] [blame] | 1057 | private int getRelatedViewBaselineOffset(int[] rules) { |
| 1058 | final View v = getRelatedView(rules, ALIGN_BASELINE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | if (v != null) { |
Alan Viverette | e1a6571 | 2015-01-14 12:30:04 -0800 | [diff] [blame] | 1060 | final int baseline = v.getBaseline(); |
| 1061 | if (baseline != -1) { |
| 1062 | final ViewGroup.LayoutParams params = v.getLayoutParams(); |
| 1063 | if (params instanceof LayoutParams) { |
| 1064 | final LayoutParams anchorParams = (LayoutParams) v.getLayoutParams(); |
| 1065 | return anchorParams.mTop + baseline; |
| 1066 | } |
| 1067 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | } |
| 1069 | return -1; |
| 1070 | } |
| 1071 | |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1072 | private static void centerHorizontal(View child, LayoutParams params, int myWidth) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | int childWidth = child.getMeasuredWidth(); |
| 1074 | int left = (myWidth - childWidth) / 2; |
| 1075 | |
| 1076 | params.mLeft = left; |
| 1077 | params.mRight = left + childWidth; |
| 1078 | } |
| 1079 | |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1080 | private static void centerVertical(View child, LayoutParams params, int myHeight) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | int childHeight = child.getMeasuredHeight(); |
| 1082 | int top = (myHeight - childHeight) / 2; |
| 1083 | |
| 1084 | params.mTop = top; |
| 1085 | params.mBottom = top + childHeight; |
| 1086 | } |
| 1087 | |
| 1088 | @Override |
| 1089 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 1090 | // The layout has actually already been performed and the positions |
| 1091 | // cached. Apply the cached values to the children. |
Fabrice Di Meglio | 13705ed | 2012-11-26 20:05:31 -0800 | [diff] [blame] | 1092 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | |
| 1094 | for (int i = 0; i < count; i++) { |
| 1095 | View child = getChildAt(i); |
| 1096 | if (child.getVisibility() != GONE) { |
| 1097 | RelativeLayout.LayoutParams st = |
| 1098 | (RelativeLayout.LayoutParams) child.getLayoutParams(); |
| 1099 | child.layout(st.mLeft, st.mTop, st.mRight, st.mBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | @Override |
| 1105 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1106 | return new RelativeLayout.LayoutParams(getContext(), attrs); |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Returns a set of layout parameters with a width of |
| 1111 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, |
| 1112 | * a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning. |
| 1113 | */ |
| 1114 | @Override |
| 1115 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
| 1116 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 1117 | } |
| 1118 | |
| 1119 | // Override to allow type-checking of LayoutParams. |
| 1120 | @Override |
| 1121 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1122 | return p instanceof RelativeLayout.LayoutParams; |
| 1123 | } |
| 1124 | |
| 1125 | @Override |
Yigit Boyar | 885c50b | 2016-03-22 16:53:42 -0700 | [diff] [blame] | 1126 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
Yigit Boyar | 2dd20a6 | 2016-08-01 17:42:28 -0700 | [diff] [blame] | 1127 | if (sPreserveMarginParamsInLayoutParamConversion) { |
| 1128 | if (lp instanceof LayoutParams) { |
| 1129 | return new LayoutParams((LayoutParams) lp); |
| 1130 | } else if (lp instanceof MarginLayoutParams) { |
| 1131 | return new LayoutParams((MarginLayoutParams) lp); |
| 1132 | } |
Yigit Boyar | 885c50b | 2016-03-22 16:53:42 -0700 | [diff] [blame] | 1133 | } |
Yigit Boyar | 2dd20a6 | 2016-08-01 17:42:28 -0700 | [diff] [blame] | 1134 | return new LayoutParams(lp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1135 | } |
| 1136 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1137 | /** @hide */ |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1138 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1139 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1140 | if (mTopToBottomLeftToRightSet == null) { |
| 1141 | mTopToBottomLeftToRightSet = new TreeSet<View>(new TopToBottomLeftToRightComparator()); |
| 1142 | } |
| 1143 | |
| 1144 | // sort children top-to-bottom and left-to-right |
| 1145 | for (int i = 0, count = getChildCount(); i < count; i++) { |
| 1146 | mTopToBottomLeftToRightSet.add(getChildAt(i)); |
| 1147 | } |
| 1148 | |
| 1149 | for (View view : mTopToBottomLeftToRightSet) { |
Svetoslav Ganov | 0b0a41d | 2011-09-07 18:06:03 -0700 | [diff] [blame] | 1150 | if (view.getVisibility() == View.VISIBLE |
| 1151 | && view.dispatchPopulateAccessibilityEvent(event)) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1152 | mTopToBottomLeftToRightSet.clear(); |
| 1153 | return true; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | mTopToBottomLeftToRightSet.clear(); |
| 1158 | return false; |
| 1159 | } |
| 1160 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1161 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1162 | public CharSequence getAccessibilityClassName() { |
| 1163 | return RelativeLayout.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1166 | /** |
| 1167 | * Compares two views in left-to-right and top-to-bottom fashion. |
| 1168 | */ |
| 1169 | private class TopToBottomLeftToRightComparator implements Comparator<View> { |
| 1170 | public int compare(View first, View second) { |
| 1171 | // top - bottom |
| 1172 | int topDifference = first.getTop() - second.getTop(); |
| 1173 | if (topDifference != 0) { |
| 1174 | return topDifference; |
| 1175 | } |
| 1176 | // left - right |
| 1177 | int leftDifference = first.getLeft() - second.getLeft(); |
| 1178 | if (leftDifference != 0) { |
| 1179 | return leftDifference; |
| 1180 | } |
| 1181 | // break tie by height |
| 1182 | int heightDiference = first.getHeight() - second.getHeight(); |
| 1183 | if (heightDiference != 0) { |
| 1184 | return heightDiference; |
| 1185 | } |
| 1186 | // break tie by width |
| 1187 | int widthDiference = first.getWidth() - second.getWidth(); |
| 1188 | if (widthDiference != 0) { |
| 1189 | return widthDiference; |
| 1190 | } |
| 1191 | return 0; |
| 1192 | } |
| 1193 | } |
| 1194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | /** |
Joe Fernandez | a1bd171 | 2017-04-25 22:28:27 -0700 | [diff] [blame] | 1196 | * Specifies how a view is positioned within a {@link RelativeLayout}. |
| 1197 | * The relative layout containing the view uses the value of these layout parameters to |
| 1198 | * determine where to position the view on the screen. If the view is not contained |
| 1199 | * within a relative layout, these attributes are ignored. |
| 1200 | * |
Andrew Solovay | 666f0fe | 2018-03-14 11:12:00 -0700 | [diff] [blame] | 1201 | * See the <a href="/guide/topics/ui/layout/relative.html"> |
Joe Fernandez | a1bd171 | 2017-04-25 22:28:27 -0700 | [diff] [blame] | 1202 | * Relative Layout</a> guide for example code demonstrating how to use relative layout’s |
| 1203 | * layout parameters in a layout XML. |
| 1204 | * |
| 1205 | * To learn more about layout parameters and how they differ from typical view attributes, |
Andrew Solovay | 666f0fe | 2018-03-14 11:12:00 -0700 | [diff] [blame] | 1206 | * see the <a href="/guide/topics/ui/declaring-layout.html#attributes"> |
Joe Fernandez | a1bd171 | 2017-04-25 22:28:27 -0700 | [diff] [blame] | 1207 | * Layouts guide</a>. |
| 1208 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | * |
| 1210 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignWithParentIfMissing |
| 1211 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toLeftOf |
| 1212 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toRightOf |
| 1213 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_above |
| 1214 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_below |
| 1215 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBaseline |
| 1216 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignLeft |
| 1217 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignTop |
| 1218 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignRight |
| 1219 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBottom |
| 1220 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentLeft |
| 1221 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentTop |
| 1222 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentRight |
| 1223 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentBottom |
| 1224 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerInParent |
| 1225 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerHorizontal |
| 1226 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerVertical |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1227 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toStartOf |
| 1228 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toEndOf |
| 1229 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignStart |
| 1230 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignEnd |
| 1231 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentStart |
| 1232 | * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentEnd |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | */ |
| 1234 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1235 | @ViewDebug.ExportedProperty(category = "layout", resolveId = true, indexMapping = { |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1236 | @ViewDebug.IntToString(from = ABOVE, to = "above"), |
| 1237 | @ViewDebug.IntToString(from = ALIGN_BASELINE, to = "alignBaseline"), |
| 1238 | @ViewDebug.IntToString(from = ALIGN_BOTTOM, to = "alignBottom"), |
| 1239 | @ViewDebug.IntToString(from = ALIGN_LEFT, to = "alignLeft"), |
| 1240 | @ViewDebug.IntToString(from = ALIGN_PARENT_BOTTOM, to = "alignParentBottom"), |
| 1241 | @ViewDebug.IntToString(from = ALIGN_PARENT_LEFT, to = "alignParentLeft"), |
| 1242 | @ViewDebug.IntToString(from = ALIGN_PARENT_RIGHT, to = "alignParentRight"), |
| 1243 | @ViewDebug.IntToString(from = ALIGN_PARENT_TOP, to = "alignParentTop"), |
| 1244 | @ViewDebug.IntToString(from = ALIGN_RIGHT, to = "alignRight"), |
| 1245 | @ViewDebug.IntToString(from = ALIGN_TOP, to = "alignTop"), |
| 1246 | @ViewDebug.IntToString(from = BELOW, to = "below"), |
| 1247 | @ViewDebug.IntToString(from = CENTER_HORIZONTAL, to = "centerHorizontal"), |
| 1248 | @ViewDebug.IntToString(from = CENTER_IN_PARENT, to = "center"), |
| 1249 | @ViewDebug.IntToString(from = CENTER_VERTICAL, to = "centerVertical"), |
| 1250 | @ViewDebug.IntToString(from = LEFT_OF, to = "leftOf"), |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1251 | @ViewDebug.IntToString(from = RIGHT_OF, to = "rightOf"), |
| 1252 | @ViewDebug.IntToString(from = ALIGN_START, to = "alignStart"), |
| 1253 | @ViewDebug.IntToString(from = ALIGN_END, to = "alignEnd"), |
| 1254 | @ViewDebug.IntToString(from = ALIGN_PARENT_START, to = "alignParentStart"), |
| 1255 | @ViewDebug.IntToString(from = ALIGN_PARENT_END, to = "alignParentEnd"), |
| 1256 | @ViewDebug.IntToString(from = START_OF, to = "startOf"), |
| 1257 | @ViewDebug.IntToString(from = END_OF, to = "endOf") |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1258 | }, mapping = { |
| 1259 | @ViewDebug.IntToString(from = TRUE, to = "true"), |
Romain Guy | a1f3e4a | 2009-06-04 15:10:46 -0700 | [diff] [blame] | 1260 | @ViewDebug.IntToString(from = 0, to = "false/NO_ID") |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1261 | }) |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | private int[] mRules = new int[VERB_COUNT]; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1264 | private int[] mInitialRules = new int[VERB_COUNT]; |
The Android Open Source Project | c39a6e0 | 2009-03-11 12:11:56 -0700 | [diff] [blame] | 1265 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1266 | @UnsupportedAppUsage |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 1267 | private int mLeft; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1268 | @UnsupportedAppUsage |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 1269 | private int mTop; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1270 | @UnsupportedAppUsage |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 1271 | private int mRight; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 1272 | @UnsupportedAppUsage |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 1273 | private int mBottom; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1274 | |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1275 | /** |
| 1276 | * Whether this view had any relative rules modified following the most |
| 1277 | * recent resolution of layout direction. |
| 1278 | */ |
| 1279 | private boolean mNeedsLayoutResolution; |
| 1280 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1281 | private boolean mRulesChanged = false; |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1282 | private boolean mIsRtlCompatibilityMode = false; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1283 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | /** |
| 1285 | * When true, uses the parent as the anchor if the anchor doesn't exist or if |
| 1286 | * the anchor's visibility is GONE. |
| 1287 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1288 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | public boolean alignWithParent; |
| 1290 | |
| 1291 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1292 | super(c, attrs); |
| 1293 | |
| 1294 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 1295 | com.android.internal.R.styleable.RelativeLayout_Layout); |
| 1296 | |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1297 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
| 1298 | mIsRtlCompatibilityMode = (targetSdkVersion < JELLY_BEAN_MR1 || |
| 1299 | !c.getApplicationInfo().hasRtlSupport()); |
| 1300 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | final int[] rules = mRules; |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1302 | //noinspection MismatchedReadAndWriteOfArray |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1303 | final int[] initialRules = mInitialRules; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | |
| 1305 | final int N = a.getIndexCount(); |
| 1306 | for (int i = 0; i < N; i++) { |
| 1307 | int attr = a.getIndex(i); |
| 1308 | switch (attr) { |
| 1309 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignWithParentIfMissing: |
| 1310 | alignWithParent = a.getBoolean(attr, false); |
| 1311 | break; |
| 1312 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toLeftOf: |
| 1313 | rules[LEFT_OF] = a.getResourceId(attr, 0); |
| 1314 | break; |
| 1315 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toRightOf: |
| 1316 | rules[RIGHT_OF] = a.getResourceId(attr, 0); |
| 1317 | break; |
| 1318 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_above: |
| 1319 | rules[ABOVE] = a.getResourceId(attr, 0); |
| 1320 | break; |
| 1321 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_below: |
| 1322 | rules[BELOW] = a.getResourceId(attr, 0); |
| 1323 | break; |
| 1324 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBaseline: |
| 1325 | rules[ALIGN_BASELINE] = a.getResourceId(attr, 0); |
| 1326 | break; |
| 1327 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignLeft: |
| 1328 | rules[ALIGN_LEFT] = a.getResourceId(attr, 0); |
| 1329 | break; |
| 1330 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignTop: |
| 1331 | rules[ALIGN_TOP] = a.getResourceId(attr, 0); |
| 1332 | break; |
| 1333 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignRight: |
| 1334 | rules[ALIGN_RIGHT] = a.getResourceId(attr, 0); |
| 1335 | break; |
| 1336 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBottom: |
| 1337 | rules[ALIGN_BOTTOM] = a.getResourceId(attr, 0); |
| 1338 | break; |
| 1339 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentLeft: |
| 1340 | rules[ALIGN_PARENT_LEFT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1341 | break; |
| 1342 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentTop: |
| 1343 | rules[ALIGN_PARENT_TOP] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1344 | break; |
| 1345 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentRight: |
| 1346 | rules[ALIGN_PARENT_RIGHT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1347 | break; |
| 1348 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentBottom: |
| 1349 | rules[ALIGN_PARENT_BOTTOM] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1350 | break; |
| 1351 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerInParent: |
| 1352 | rules[CENTER_IN_PARENT] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1353 | break; |
| 1354 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerHorizontal: |
| 1355 | rules[CENTER_HORIZONTAL] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1356 | break; |
| 1357 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerVertical: |
| 1358 | rules[CENTER_VERTICAL] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1359 | break; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1360 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toStartOf: |
| 1361 | rules[START_OF] = a.getResourceId(attr, 0); |
| 1362 | break; |
| 1363 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toEndOf: |
| 1364 | rules[END_OF] = a.getResourceId(attr, 0); |
| 1365 | break; |
| 1366 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignStart: |
| 1367 | rules[ALIGN_START] = a.getResourceId(attr, 0); |
| 1368 | break; |
| 1369 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignEnd: |
| 1370 | rules[ALIGN_END] = a.getResourceId(attr, 0); |
| 1371 | break; |
| 1372 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentStart: |
| 1373 | rules[ALIGN_PARENT_START] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1374 | break; |
| 1375 | case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentEnd: |
| 1376 | rules[ALIGN_PARENT_END] = a.getBoolean(attr, false) ? TRUE : 0; |
| 1377 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1378 | } |
| 1379 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 1380 | mRulesChanged = true; |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1381 | System.arraycopy(rules, LEFT_OF, initialRules, LEFT_OF, VERB_COUNT); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | a.recycle(); |
| 1384 | } |
| 1385 | |
| 1386 | public LayoutParams(int w, int h) { |
| 1387 | super(w, h); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * {@inheritDoc} |
| 1392 | */ |
| 1393 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 1394 | super(source); |
| 1395 | } |
| 1396 | |
| 1397 | /** |
| 1398 | * {@inheritDoc} |
| 1399 | */ |
| 1400 | public LayoutParams(ViewGroup.MarginLayoutParams source) { |
| 1401 | super(source); |
| 1402 | } |
| 1403 | |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 1404 | /** |
| 1405 | * Copy constructor. Clones the width, height, margin values, and rules |
| 1406 | * of the source. |
| 1407 | * |
| 1408 | * @param source The layout params to copy from. |
| 1409 | */ |
| 1410 | public LayoutParams(LayoutParams source) { |
| 1411 | super(source); |
| 1412 | |
| 1413 | this.mIsRtlCompatibilityMode = source.mIsRtlCompatibilityMode; |
| 1414 | this.mRulesChanged = source.mRulesChanged; |
| 1415 | this.alignWithParent = source.alignWithParent; |
| 1416 | |
| 1417 | System.arraycopy(source.mRules, LEFT_OF, this.mRules, LEFT_OF, VERB_COUNT); |
| 1418 | System.arraycopy( |
| 1419 | source.mInitialRules, LEFT_OF, this.mInitialRules, LEFT_OF, VERB_COUNT); |
| 1420 | } |
| 1421 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | @Override |
| 1423 | public String debug(String output) { |
| 1424 | return output + "ViewGroup.LayoutParams={ width=" + sizeToString(width) + |
| 1425 | ", height=" + sizeToString(height) + " }"; |
| 1426 | } |
| 1427 | |
| 1428 | /** |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1429 | * Adds a layout rule to be interpreted by the RelativeLayout. |
| 1430 | * <p> |
| 1431 | * This method should only be used for verbs that don't refer to a |
| 1432 | * sibling (ex. {@link #ALIGN_RIGHT}) or take a boolean |
| 1433 | * value ({@link #TRUE} for true or 0 for false). To |
| 1434 | * specify a verb that takes a subject, use {@link #addRule(int, int)}. |
| 1435 | * <p> |
| 1436 | * If the rule is relative to the layout direction (ex. |
| 1437 | * {@link #ALIGN_PARENT_START}), then the layout direction must be |
| 1438 | * resolved using {@link #resolveLayoutDirection(int)} before calling |
| 1439 | * {@link #getRule(int)} an absolute rule (ex. |
| 1440 | * {@link #ALIGN_PARENT_LEFT}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | * |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1442 | * @param verb a layout verb, such as {@link #ALIGN_PARENT_LEFT} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1443 | * @see #addRule(int, int) |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1444 | * @see #removeRule(int) |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 1445 | * @see #getRule(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | */ |
| 1447 | public void addRule(int verb) { |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1448 | addRule(verb, TRUE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | /** |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1452 | * Adds a layout rule to be interpreted by the RelativeLayout. |
| 1453 | * <p> |
| 1454 | * Use this for verbs that refer to a sibling (ex. |
| 1455 | * {@link #ALIGN_RIGHT}) or take a boolean value (ex. |
| 1456 | * {@link #CENTER_IN_PARENT}). |
| 1457 | * <p> |
| 1458 | * If the rule is relative to the layout direction (ex. |
| 1459 | * {@link #START_OF}), then the layout direction must be resolved using |
| 1460 | * {@link #resolveLayoutDirection(int)} before calling |
| 1461 | * {@link #getRule(int)} with an absolute rule (ex. {@link #LEFT_OF}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | * |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1463 | * @param verb a layout verb, such as {@link #ALIGN_RIGHT} |
| 1464 | * @param subject the ID of another view to use as an anchor, or a |
| 1465 | * boolean value (represented as {@link #TRUE} for true |
| 1466 | * or 0 for false) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1467 | * @see #addRule(int) |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1468 | * @see #removeRule(int) |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 1469 | * @see #getRule(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | */ |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1471 | public void addRule(int verb, int subject) { |
| 1472 | // If we're removing a relative rule, we'll need to force layout |
| 1473 | // resolution the next time it's requested. |
| 1474 | if (!mNeedsLayoutResolution && isRelativeRule(verb) |
| 1475 | && mInitialRules[verb] != 0 && subject == 0) { |
| 1476 | mNeedsLayoutResolution = true; |
| 1477 | } |
| 1478 | |
| 1479 | mRules[verb] = subject; |
| 1480 | mInitialRules[verb] = subject; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1481 | mRulesChanged = true; |
| 1482 | } |
| 1483 | |
Fabrice Di Meglio | a4c1030 | 2012-07-26 12:27:56 -0700 | [diff] [blame] | 1484 | /** |
| 1485 | * Removes a layout rule to be interpreted by the RelativeLayout. |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1486 | * <p> |
| 1487 | * If the rule is relative to the layout direction (ex. |
| 1488 | * {@link #START_OF}, {@link #ALIGN_PARENT_START}, etc.) then the |
| 1489 | * layout direction must be resolved using |
| 1490 | * {@link #resolveLayoutDirection(int)} before before calling |
| 1491 | * {@link #getRule(int)} with an absolute rule (ex. {@link #LEFT_OF}. |
Fabrice Di Meglio | a4c1030 | 2012-07-26 12:27:56 -0700 | [diff] [blame] | 1492 | * |
| 1493 | * @param verb One of the verbs defined by |
| 1494 | * {@link android.widget.RelativeLayout RelativeLayout}, such as |
| 1495 | * ALIGN_WITH_PARENT_LEFT. |
| 1496 | * @see #addRule(int) |
| 1497 | * @see #addRule(int, int) |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 1498 | * @see #getRule(int) |
Fabrice Di Meglio | a4c1030 | 2012-07-26 12:27:56 -0700 | [diff] [blame] | 1499 | */ |
| 1500 | public void removeRule(int verb) { |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1501 | addRule(verb, 0); |
Fabrice Di Meglio | a4c1030 | 2012-07-26 12:27:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Alan Viverette | 62c79e9 | 2015-02-26 09:47:10 -0800 | [diff] [blame] | 1504 | /** |
| 1505 | * Returns the layout rule associated with a specific verb. |
| 1506 | * |
| 1507 | * @param verb one of the verbs defined by {@link RelativeLayout}, such |
| 1508 | * as ALIGN_WITH_PARENT_LEFT |
| 1509 | * @return the id of another view to use as an anchor, a boolean value |
| 1510 | * (represented as {@link RelativeLayout#TRUE} for true |
| 1511 | * or 0 for false), or -1 for verbs that don't refer to another |
| 1512 | * sibling (for example, ALIGN_WITH_PARENT_BOTTOM) |
| 1513 | * @see #addRule(int) |
| 1514 | * @see #addRule(int, int) |
| 1515 | */ |
| 1516 | public int getRule(int verb) { |
| 1517 | return mRules[verb]; |
| 1518 | } |
| 1519 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1520 | private boolean hasRelativeRules() { |
| 1521 | return (mInitialRules[START_OF] != 0 || mInitialRules[END_OF] != 0 || |
| 1522 | mInitialRules[ALIGN_START] != 0 || mInitialRules[ALIGN_END] != 0 || |
| 1523 | mInitialRules[ALIGN_PARENT_START] != 0 || mInitialRules[ALIGN_PARENT_END] != 0); |
| 1524 | } |
| 1525 | |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1526 | private boolean isRelativeRule(int rule) { |
| 1527 | return rule == START_OF || rule == END_OF |
| 1528 | || rule == ALIGN_START || rule == ALIGN_END |
| 1529 | || rule == ALIGN_PARENT_START || rule == ALIGN_PARENT_END; |
| 1530 | } |
| 1531 | |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1532 | // The way we are resolving rules depends on the layout direction and if we are pre JB MR1 |
| 1533 | // or not. |
| 1534 | // |
| 1535 | // If we are pre JB MR1 (said as "RTL compatibility mode"), "left"/"right" rules are having |
| 1536 | // predominance over any "start/end" rules that could have been defined. A special case: |
| 1537 | // if no "left"/"right" rule has been defined and "start"/"end" rules are defined then we |
| 1538 | // resolve those "start"/"end" rules to "left"/"right" respectively. |
| 1539 | // |
| 1540 | // If we are JB MR1+, then "start"/"end" rules are having predominance over "left"/"right" |
| 1541 | // rules. If no "start"/"end" rule is defined then we use "left"/"right" rules. |
| 1542 | // |
| 1543 | // In all cases, the result of the resolution should clear the "start"/"end" rules to leave |
| 1544 | // only the "left"/"right" rules at the end. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1545 | private void resolveRules(int layoutDirection) { |
| 1546 | final boolean isLayoutRtl = (layoutDirection == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1547 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1548 | // Reset to initial state |
Romain Guy | f16c7a9 | 2013-02-11 17:43:59 -0800 | [diff] [blame] | 1549 | System.arraycopy(mInitialRules, LEFT_OF, mRules, LEFT_OF, VERB_COUNT); |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1550 | |
| 1551 | // Apply rules depending on direction and if we are in RTL compatibility mode |
| 1552 | if (mIsRtlCompatibilityMode) { |
| 1553 | if (mRules[ALIGN_START] != 0) { |
| 1554 | if (mRules[ALIGN_LEFT] == 0) { |
| 1555 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1556 | // the "left" rule |
| 1557 | mRules[ALIGN_LEFT] = mRules[ALIGN_START]; |
| 1558 | } |
| 1559 | mRules[ALIGN_START] = 0; |
| 1560 | } |
| 1561 | |
| 1562 | if (mRules[ALIGN_END] != 0) { |
| 1563 | if (mRules[ALIGN_RIGHT] == 0) { |
| 1564 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1565 | // "right" rule |
| 1566 | mRules[ALIGN_RIGHT] = mRules[ALIGN_END]; |
| 1567 | } |
| 1568 | mRules[ALIGN_END] = 0; |
| 1569 | } |
| 1570 | |
| 1571 | if (mRules[START_OF] != 0) { |
| 1572 | if (mRules[LEFT_OF] == 0) { |
| 1573 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1574 | // the "left" rule |
| 1575 | mRules[LEFT_OF] = mRules[START_OF]; |
| 1576 | } |
| 1577 | mRules[START_OF] = 0; |
| 1578 | } |
| 1579 | |
| 1580 | if (mRules[END_OF] != 0) { |
| 1581 | if (mRules[RIGHT_OF] == 0) { |
| 1582 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1583 | // "right" rule |
| 1584 | mRules[RIGHT_OF] = mRules[END_OF]; |
| 1585 | } |
| 1586 | mRules[END_OF] = 0; |
| 1587 | } |
| 1588 | |
| 1589 | if (mRules[ALIGN_PARENT_START] != 0) { |
| 1590 | if (mRules[ALIGN_PARENT_LEFT] == 0) { |
| 1591 | // "left" rule is not defined but "start" rule is: use the "start" rule as |
| 1592 | // the "left" rule |
| 1593 | mRules[ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START]; |
| 1594 | } |
| 1595 | mRules[ALIGN_PARENT_START] = 0; |
| 1596 | } |
| 1597 | |
Alan Viverette | 567e447 | 2014-08-19 15:41:51 -0700 | [diff] [blame] | 1598 | if (mRules[ALIGN_PARENT_END] != 0) { |
Fabrice Di Meglio | c44d880 | 2013-02-19 17:42:13 -0800 | [diff] [blame] | 1599 | if (mRules[ALIGN_PARENT_RIGHT] == 0) { |
| 1600 | // "right" rule is not defined but "end" rule is: use the "end" rule as the |
| 1601 | // "right" rule |
| 1602 | mRules[ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END]; |
| 1603 | } |
| 1604 | mRules[ALIGN_PARENT_END] = 0; |
| 1605 | } |
| 1606 | } else { |
| 1607 | // JB MR1+ case |
| 1608 | if ((mRules[ALIGN_START] != 0 || mRules[ALIGN_END] != 0) && |
| 1609 | (mRules[ALIGN_LEFT] != 0 || mRules[ALIGN_RIGHT] != 0)) { |
| 1610 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1611 | mRules[ALIGN_LEFT] = 0; |
| 1612 | mRules[ALIGN_RIGHT] = 0; |
| 1613 | } |
| 1614 | if (mRules[ALIGN_START] != 0) { |
| 1615 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1616 | mRules[isLayoutRtl ? ALIGN_RIGHT : ALIGN_LEFT] = mRules[ALIGN_START]; |
| 1617 | mRules[ALIGN_START] = 0; |
| 1618 | } |
| 1619 | if (mRules[ALIGN_END] != 0) { |
| 1620 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1621 | mRules[isLayoutRtl ? ALIGN_LEFT : ALIGN_RIGHT] = mRules[ALIGN_END]; |
| 1622 | mRules[ALIGN_END] = 0; |
| 1623 | } |
| 1624 | |
| 1625 | if ((mRules[START_OF] != 0 || mRules[END_OF] != 0) && |
| 1626 | (mRules[LEFT_OF] != 0 || mRules[RIGHT_OF] != 0)) { |
| 1627 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1628 | mRules[LEFT_OF] = 0; |
| 1629 | mRules[RIGHT_OF] = 0; |
| 1630 | } |
| 1631 | if (mRules[START_OF] != 0) { |
| 1632 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1633 | mRules[isLayoutRtl ? RIGHT_OF : LEFT_OF] = mRules[START_OF]; |
| 1634 | mRules[START_OF] = 0; |
| 1635 | } |
| 1636 | if (mRules[END_OF] != 0) { |
| 1637 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1638 | mRules[isLayoutRtl ? LEFT_OF : RIGHT_OF] = mRules[END_OF]; |
| 1639 | mRules[END_OF] = 0; |
| 1640 | } |
| 1641 | |
| 1642 | if ((mRules[ALIGN_PARENT_START] != 0 || mRules[ALIGN_PARENT_END] != 0) && |
| 1643 | (mRules[ALIGN_PARENT_LEFT] != 0 || mRules[ALIGN_PARENT_RIGHT] != 0)) { |
| 1644 | // "start"/"end" rules take precedence over "left"/"right" rules |
| 1645 | mRules[ALIGN_PARENT_LEFT] = 0; |
| 1646 | mRules[ALIGN_PARENT_RIGHT] = 0; |
| 1647 | } |
| 1648 | if (mRules[ALIGN_PARENT_START] != 0) { |
| 1649 | // "start" rule resolved to "left" or "right" depending on the direction |
| 1650 | mRules[isLayoutRtl ? ALIGN_PARENT_RIGHT : ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START]; |
| 1651 | mRules[ALIGN_PARENT_START] = 0; |
| 1652 | } |
| 1653 | if (mRules[ALIGN_PARENT_END] != 0) { |
| 1654 | // "end" rule resolved to "left" or "right" depending on the direction |
| 1655 | mRules[isLayoutRtl ? ALIGN_PARENT_LEFT : ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END]; |
| 1656 | mRules[ALIGN_PARENT_END] = 0; |
| 1657 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1658 | } |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1659 | |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1660 | mRulesChanged = false; |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1661 | mNeedsLayoutResolution = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * Retrieves a complete list of all supported rules, where the index is the rule |
| 1666 | * verb, and the element value is the value specified, or "false" if it was never |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1667 | * set. If there are relative rules defined (*_START / *_END), they will be resolved |
| 1668 | * depending on the layout direction. |
| 1669 | * |
| 1670 | * @param layoutDirection the direction of the layout. |
| 1671 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 1672 | * or {@link View#LAYOUT_DIRECTION_RTL} |
| 1673 | * @return the supported rules |
| 1674 | * @see #addRule(int, int) |
| 1675 | * |
| 1676 | * @hide |
| 1677 | */ |
| 1678 | public int[] getRules(int layoutDirection) { |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1679 | resolveLayoutDirection(layoutDirection); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1680 | return mRules; |
| 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * Retrieves a complete list of all supported rules, where the index is the rule |
| 1685 | * verb, and the element value is the value specified, or "false" if it was never |
| 1686 | * set. There will be no resolution of relative rules done. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1687 | * |
| 1688 | * @return the supported rules |
| 1689 | * @see #addRule(int, int) |
| 1690 | */ |
| 1691 | public int[] getRules() { |
| 1692 | return mRules; |
| 1693 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1694 | |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1695 | /** |
| 1696 | * This will be called by {@link android.view.View#requestLayout()} to |
| 1697 | * resolve layout parameters that are relative to the layout direction. |
| 1698 | * <p> |
| 1699 | * After this method is called, any rules using layout-relative verbs |
| 1700 | * (ex. {@link #START_OF}) previously added via {@link #addRule(int)} |
| 1701 | * may only be accessed via their resolved absolute verbs (ex. |
| 1702 | * {@link #LEFT_OF}). |
| 1703 | */ |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1704 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 1705 | public void resolveLayoutDirection(int layoutDirection) { |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1706 | if (shouldResolveLayoutDirection(layoutDirection)) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1707 | resolveRules(layoutDirection); |
| 1708 | } |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1709 | |
| 1710 | // This will set the layout direction. |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 1711 | super.resolveLayoutDirection(layoutDirection); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 1712 | } |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 1713 | |
Alan Viverette | 895615c | 2015-08-18 14:51:48 -0400 | [diff] [blame] | 1714 | private boolean shouldResolveLayoutDirection(int layoutDirection) { |
| 1715 | return (mNeedsLayoutResolution || hasRelativeRules()) |
| 1716 | && (mRulesChanged || layoutDirection != getLayoutDirection()); |
| 1717 | } |
| 1718 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 1719 | /** @hide */ |
| 1720 | @Override |
| 1721 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 1722 | super.encodeProperties(encoder); |
| 1723 | encoder.addProperty("layout:alignWithParent", alignWithParent); |
| 1724 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1725 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1726 | |
| 1727 | private static class DependencyGraph { |
| 1728 | /** |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1729 | * List of all views in the graph. |
| 1730 | */ |
| 1731 | private ArrayList<Node> mNodes = new ArrayList<Node>(); |
| 1732 | |
| 1733 | /** |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1734 | * List of nodes in the graph. Each node is identified by its |
| 1735 | * view id (see View#getId()). |
| 1736 | */ |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1737 | private SparseArray<Node> mKeyNodes = new SparseArray<Node>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1738 | |
| 1739 | /** |
| 1740 | * Temporary data structure used to build the list of roots |
| 1741 | * for this graph. |
| 1742 | */ |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1743 | private ArrayDeque<Node> mRoots = new ArrayDeque<Node>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1744 | |
| 1745 | /** |
| 1746 | * Clears the graph. |
| 1747 | */ |
| 1748 | void clear() { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1749 | final ArrayList<Node> nodes = mNodes; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1750 | final int count = nodes.size(); |
| 1751 | |
| 1752 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1753 | nodes.get(i).release(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1754 | } |
| 1755 | nodes.clear(); |
| 1756 | |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1757 | mKeyNodes.clear(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1758 | mRoots.clear(); |
| 1759 | } |
| 1760 | |
| 1761 | /** |
| 1762 | * Adds a view to the graph. |
| 1763 | * |
| 1764 | * @param view The view to be added as a node to the graph. |
| 1765 | */ |
| 1766 | void add(View view) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1767 | final int id = view.getId(); |
| 1768 | final Node node = Node.acquire(view); |
| 1769 | |
| 1770 | if (id != View.NO_ID) { |
| 1771 | mKeyNodes.put(id, node); |
| 1772 | } |
| 1773 | |
| 1774 | mNodes.add(node); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | /** |
| 1778 | * Builds a sorted list of views. The sorting order depends on the dependencies |
| 1779 | * between the view. For instance, if view C needs view A to be processed first |
| 1780 | * and view A needs view B to be processed first, the dependency graph |
| 1781 | * is: B -> A -> C. The sorted array will contain views B, A and C in this order. |
| 1782 | * |
| 1783 | * @param sorted The sorted list of views. The length of this array must |
| 1784 | * be equal to getChildCount(). |
| 1785 | * @param rules The list of rules to take into account. |
| 1786 | */ |
| 1787 | void getSortedViews(View[] sorted, int... rules) { |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1788 | final ArrayDeque<Node> roots = findRoots(rules); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1789 | int index = 0; |
| 1790 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1791 | Node node; |
| 1792 | while ((node = roots.pollLast()) != null) { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1793 | final View view = node.view; |
| 1794 | final int key = view.getId(); |
| 1795 | |
| 1796 | sorted[index++] = view; |
| 1797 | |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 1798 | final ArrayMap<Node, DependencyGraph> dependents = node.dependents; |
| 1799 | final int count = dependents.size(); |
| 1800 | for (int i = 0; i < count; i++) { |
| 1801 | final Node dependent = dependents.keyAt(i); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1802 | final SparseArray<Node> dependencies = dependent.dependencies; |
| 1803 | |
| 1804 | dependencies.remove(key); |
| 1805 | if (dependencies.size() == 0) { |
| 1806 | roots.add(dependent); |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | if (index < sorted.length) { |
| 1812 | throw new IllegalStateException("Circular dependencies cannot exist" |
| 1813 | + " in RelativeLayout"); |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | /** |
| 1818 | * Finds the roots of the graph. A root is a node with no dependency and |
| 1819 | * with [0..n] dependents. |
| 1820 | * |
| 1821 | * @param rulesFilter The list of rules to consider when building the |
| 1822 | * dependencies |
| 1823 | * |
| 1824 | * @return A list of node, each being a root of the graph |
| 1825 | */ |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1826 | private ArrayDeque<Node> findRoots(int[] rulesFilter) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1827 | final SparseArray<Node> keyNodes = mKeyNodes; |
| 1828 | final ArrayList<Node> nodes = mNodes; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1829 | final int count = nodes.size(); |
| 1830 | |
| 1831 | // Find roots can be invoked several times, so make sure to clear |
| 1832 | // all dependents and dependencies before running the algorithm |
| 1833 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1834 | final Node node = nodes.get(i); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1835 | node.dependents.clear(); |
| 1836 | node.dependencies.clear(); |
| 1837 | } |
| 1838 | |
| 1839 | // Builds up the dependents and dependencies for each node of the graph |
| 1840 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1841 | final Node node = nodes.get(i); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1842 | |
| 1843 | final LayoutParams layoutParams = (LayoutParams) node.view.getLayoutParams(); |
| 1844 | final int[] rules = layoutParams.mRules; |
| 1845 | final int rulesCount = rulesFilter.length; |
| 1846 | |
| 1847 | // Look only the the rules passed in parameter, this way we build only the |
| 1848 | // dependencies for a specific set of rules |
| 1849 | for (int j = 0; j < rulesCount; j++) { |
| 1850 | final int rule = rules[rulesFilter[j]]; |
| 1851 | if (rule > 0) { |
| 1852 | // The node this node depends on |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1853 | final Node dependency = keyNodes.get(rule); |
Romain Guy | da3003e | 2009-07-19 19:47:42 -0700 | [diff] [blame] | 1854 | // Skip unknowns and self dependencies |
| 1855 | if (dependency == null || dependency == node) { |
Romain Guy | b8f8de8 | 2009-06-25 12:03:56 -0700 | [diff] [blame] | 1856 | continue; |
| 1857 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1858 | // Add the current node as a dependent |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1859 | dependency.dependents.put(node, this); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1860 | // Add a dependency to the current node |
| 1861 | node.dependencies.put(rule, dependency); |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1866 | final ArrayDeque<Node> roots = mRoots; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1867 | roots.clear(); |
| 1868 | |
| 1869 | // Finds all the roots in the graph: all nodes with no dependencies |
| 1870 | for (int i = 0; i < count; i++) { |
Romain Guy | 1ab621e | 2009-06-25 13:31:57 -0700 | [diff] [blame] | 1871 | final Node node = nodes.get(i); |
Romain Guy | bc5d876 | 2012-01-06 16:40:49 -0800 | [diff] [blame] | 1872 | if (node.dependencies.size() == 0) roots.addLast(node); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | return roots; |
| 1876 | } |
| 1877 | |
| 1878 | /** |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1879 | * A node in the dependency graph. A node is a view, its list of dependencies |
| 1880 | * and its list of dependents. |
| 1881 | * |
| 1882 | * A node with no dependent is considered a root of the graph. |
| 1883 | */ |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1884 | static class Node { |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1885 | /** |
| 1886 | * The view representing this node in the layout. |
| 1887 | */ |
| 1888 | View view; |
| 1889 | |
| 1890 | /** |
| 1891 | * The list of dependents for this node; a dependent is a node |
| 1892 | * that needs this node to be processed first. |
| 1893 | */ |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 1894 | final ArrayMap<Node, DependencyGraph> dependents = |
| 1895 | new ArrayMap<Node, DependencyGraph>(); |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1896 | |
| 1897 | /** |
| 1898 | * The list of dependencies for this node. |
| 1899 | */ |
| 1900 | final SparseArray<Node> dependencies = new SparseArray<Node>(); |
| 1901 | |
| 1902 | /* |
| 1903 | * START POOL IMPLEMENTATION |
| 1904 | */ |
Romain Guy | baac463 | 2009-06-29 14:28:29 -0700 | [diff] [blame] | 1905 | // The pool is static, so all nodes instances are shared across |
| 1906 | // activities, that's why we give it a rather high limit |
| 1907 | private static final int POOL_LIMIT = 100; |
Romain Guy | 6876b4f | 2013-06-03 14:25:56 -0700 | [diff] [blame] | 1908 | private static final SynchronizedPool<Node> sPool = |
| 1909 | new SynchronizedPool<Node>(POOL_LIMIT); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1910 | |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1911 | static Node acquire(View view) { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 1912 | Node node = sPool.acquire(); |
| 1913 | if (node == null) { |
| 1914 | node = new Node(); |
| 1915 | } |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1916 | node.view = view; |
Romain Guy | 725015a | 2009-06-23 14:27:34 -0700 | [diff] [blame] | 1917 | return node; |
| 1918 | } |
| 1919 | |
| 1920 | void release() { |
| 1921 | view = null; |
| 1922 | dependents.clear(); |
| 1923 | dependencies.clear(); |
| 1924 | |
| 1925 | sPool.release(this); |
| 1926 | } |
| 1927 | /* |
| 1928 | * END POOL IMPLEMENTATION |
| 1929 | */ |
| 1930 | } |
| 1931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | } |