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 | |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Scott Kennedy | 3a1fa10 | 2015-03-05 19:15:11 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.TypedArray; |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.Gravity; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewDebug; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.widget.RemoteViews.RemoteView; |
| 33 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 34 | import java.lang.annotation.Retention; |
| 35 | import java.lang.annotation.RetentionPolicy; |
| 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * A Layout that arranges its children in a single column or a single row. The direction of |
| 40 | * the row can be set by calling {@link #setOrientation(int) setOrientation()}. |
| 41 | * You can also specify gravity, which specifies the alignment of all the child elements by |
| 42 | * calling {@link #setGravity(int) setGravity()} or specify that specific children |
| 43 | * grow to fill up any remaining space in the layout by setting the <em>weight</em> member of |
| 44 | * {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams}. |
| 45 | * The default orientation is horizontal. |
| 46 | * |
Scott Main | 4c359b7 | 2012-07-24 15:51:27 -0700 | [diff] [blame] | 47 | * <p>See the <a href="{@docRoot}guide/topics/ui/layout/linear.html">Linear Layout</a> |
| 48 | * guide.</p> |
Scott Main | 41ec653 | 2010-08-19 16:57:07 -0700 | [diff] [blame] | 49 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * <p> |
| 51 | * Also see {@link LinearLayout.LayoutParams android.widget.LinearLayout.LayoutParams} |
| 52 | * for layout attributes </p> |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 53 | * |
| 54 | * @attr ref android.R.styleable#LinearLayout_baselineAligned |
| 55 | * @attr ref android.R.styleable#LinearLayout_baselineAlignedChildIndex |
| 56 | * @attr ref android.R.styleable#LinearLayout_gravity |
| 57 | * @attr ref android.R.styleable#LinearLayout_measureWithLargestChild |
| 58 | * @attr ref android.R.styleable#LinearLayout_orientation |
| 59 | * @attr ref android.R.styleable#LinearLayout_weightSum |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
| 61 | @RemoteView |
| 62 | public class LinearLayout extends ViewGroup { |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 63 | /** @hide */ |
| 64 | @IntDef({HORIZONTAL, VERTICAL}) |
| 65 | @Retention(RetentionPolicy.SOURCE) |
| 66 | public @interface OrientationMode {} |
| 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | public static final int HORIZONTAL = 0; |
| 69 | public static final int VERTICAL = 1; |
| 70 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 71 | /** @hide */ |
| 72 | @IntDef(flag = true, |
| 73 | value = { |
| 74 | SHOW_DIVIDER_NONE, |
| 75 | SHOW_DIVIDER_BEGINNING, |
| 76 | SHOW_DIVIDER_MIDDLE, |
| 77 | SHOW_DIVIDER_END |
| 78 | }) |
| 79 | @Retention(RetentionPolicy.SOURCE) |
| 80 | public @interface DividerMode {} |
| 81 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | /** |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 83 | * Don't show any dividers. |
| 84 | */ |
| 85 | public static final int SHOW_DIVIDER_NONE = 0; |
| 86 | /** |
| 87 | * Show a divider at the beginning of the group. |
| 88 | */ |
| 89 | public static final int SHOW_DIVIDER_BEGINNING = 1; |
| 90 | /** |
| 91 | * Show dividers between each item in the group. |
| 92 | */ |
| 93 | public static final int SHOW_DIVIDER_MIDDLE = 2; |
| 94 | /** |
| 95 | * Show a divider at the end of the group. |
| 96 | */ |
| 97 | public static final int SHOW_DIVIDER_END = 4; |
| 98 | |
| 99 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * Whether the children of this layout are baseline aligned. Only applicable |
| 101 | * if {@link #mOrientation} is horizontal. |
| 102 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 103 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | private boolean mBaselineAligned = true; |
| 105 | |
| 106 | /** |
| 107 | * If this layout is part of another layout that is baseline aligned, |
| 108 | * use the child at this index as the baseline. |
| 109 | * |
| 110 | * Note: this is orthogonal to {@link #mBaselineAligned}, which is concerned |
| 111 | * with whether the children of this layout are baseline aligned. |
| 112 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 113 | @ViewDebug.ExportedProperty(category = "layout") |
Karl Rosaen | c1f9c40 | 2009-08-12 17:18:33 -0700 | [diff] [blame] | 114 | private int mBaselineAlignedChildIndex = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * The additional offset to the child's baseline. |
| 118 | * We'll calculate the baseline of this layout as we measure vertically; for |
| 119 | * horizontal linear layouts, the offset of 0 is appropriate. |
| 120 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 121 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | private int mBaselineChildTop = 0; |
| 123 | |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 124 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | private int mOrientation; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 126 | |
Fabrice Di Meglio | bce84d2 | 2011-06-02 15:57:01 -0700 | [diff] [blame] | 127 | @ViewDebug.ExportedProperty(category = "measurement", flagMapping = { |
| 128 | @ViewDebug.FlagToString(mask = -1, |
| 129 | equals = -1, name = "NONE"), |
| 130 | @ViewDebug.FlagToString(mask = Gravity.NO_GRAVITY, |
| 131 | equals = Gravity.NO_GRAVITY,name = "NONE"), |
| 132 | @ViewDebug.FlagToString(mask = Gravity.TOP, |
| 133 | equals = Gravity.TOP, name = "TOP"), |
| 134 | @ViewDebug.FlagToString(mask = Gravity.BOTTOM, |
| 135 | equals = Gravity.BOTTOM, name = "BOTTOM"), |
| 136 | @ViewDebug.FlagToString(mask = Gravity.LEFT, |
| 137 | equals = Gravity.LEFT, name = "LEFT"), |
| 138 | @ViewDebug.FlagToString(mask = Gravity.RIGHT, |
| 139 | equals = Gravity.RIGHT, name = "RIGHT"), |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 140 | @ViewDebug.FlagToString(mask = Gravity.START, |
| 141 | equals = Gravity.START, name = "START"), |
| 142 | @ViewDebug.FlagToString(mask = Gravity.END, |
| 143 | equals = Gravity.END, name = "END"), |
Fabrice Di Meglio | bce84d2 | 2011-06-02 15:57:01 -0700 | [diff] [blame] | 144 | @ViewDebug.FlagToString(mask = Gravity.CENTER_VERTICAL, |
| 145 | equals = Gravity.CENTER_VERTICAL, name = "CENTER_VERTICAL"), |
| 146 | @ViewDebug.FlagToString(mask = Gravity.FILL_VERTICAL, |
| 147 | equals = Gravity.FILL_VERTICAL, name = "FILL_VERTICAL"), |
| 148 | @ViewDebug.FlagToString(mask = Gravity.CENTER_HORIZONTAL, |
| 149 | equals = Gravity.CENTER_HORIZONTAL, name = "CENTER_HORIZONTAL"), |
| 150 | @ViewDebug.FlagToString(mask = Gravity.FILL_HORIZONTAL, |
| 151 | equals = Gravity.FILL_HORIZONTAL, name = "FILL_HORIZONTAL"), |
| 152 | @ViewDebug.FlagToString(mask = Gravity.CENTER, |
| 153 | equals = Gravity.CENTER, name = "CENTER"), |
| 154 | @ViewDebug.FlagToString(mask = Gravity.FILL, |
| 155 | equals = Gravity.FILL, name = "FILL"), |
Fabrice Di Meglio | c46f7ff | 2011-06-06 18:23:10 -0700 | [diff] [blame] | 156 | @ViewDebug.FlagToString(mask = Gravity.RELATIVE_LAYOUT_DIRECTION, |
| 157 | equals = Gravity.RELATIVE_LAYOUT_DIRECTION, name = "RELATIVE") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 158 | }, formatToHexString = true) |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 159 | private int mGravity = Gravity.START | Gravity.TOP; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 160 | |
| 161 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | private int mTotalLength; |
| 163 | |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 164 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | private float mWeightSum; |
| 166 | |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 167 | @ViewDebug.ExportedProperty(category = "layout") |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 168 | private boolean mUseLargestChild; |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | private int[] mMaxAscent; |
| 171 | private int[] mMaxDescent; |
| 172 | |
| 173 | private static final int VERTICAL_GRAVITY_COUNT = 4; |
| 174 | |
| 175 | private static final int INDEX_CENTER_VERTICAL = 0; |
| 176 | private static final int INDEX_TOP = 1; |
| 177 | private static final int INDEX_BOTTOM = 2; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 178 | private static final int INDEX_FILL = 3; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 180 | private Drawable mDivider; |
| 181 | private int mDividerWidth; |
| 182 | private int mDividerHeight; |
| 183 | private int mShowDividers; |
| 184 | private int mDividerPadding; |
| 185 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | public LinearLayout(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 187 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Scott Kennedy | 3a1fa10 | 2015-03-05 19:15:11 -0800 | [diff] [blame] | 190 | public LinearLayout(Context context, @Nullable AttributeSet attrs) { |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 191 | this(context, attrs, 0); |
| 192 | } |
| 193 | |
Scott Kennedy | 3a1fa10 | 2015-03-05 19:15:11 -0800 | [diff] [blame] | 194 | public LinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 195 | this(context, attrs, defStyleAttr, 0); |
| 196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 198 | public LinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 199 | super(context, attrs, defStyleAttr, defStyleRes); |
| 200 | |
| 201 | final TypedArray a = context.obtainStyledAttributes( |
| 202 | attrs, com.android.internal.R.styleable.LinearLayout, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
| 204 | int index = a.getInt(com.android.internal.R.styleable.LinearLayout_orientation, -1); |
| 205 | if (index >= 0) { |
| 206 | setOrientation(index); |
| 207 | } |
| 208 | |
| 209 | index = a.getInt(com.android.internal.R.styleable.LinearLayout_gravity, -1); |
| 210 | if (index >= 0) { |
| 211 | setGravity(index); |
| 212 | } |
| 213 | |
| 214 | boolean baselineAligned = a.getBoolean(R.styleable.LinearLayout_baselineAligned, true); |
| 215 | if (!baselineAligned) { |
| 216 | setBaselineAligned(baselineAligned); |
| 217 | } |
| 218 | |
| 219 | mWeightSum = a.getFloat(R.styleable.LinearLayout_weightSum, -1.0f); |
| 220 | |
| 221 | mBaselineAlignedChildIndex = |
| 222 | a.getInt(com.android.internal.R.styleable.LinearLayout_baselineAlignedChildIndex, -1); |
| 223 | |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 224 | mUseLargestChild = a.getBoolean(R.styleable.LinearLayout_measureWithLargestChild, false); |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 225 | |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 226 | setDividerDrawable(a.getDrawable(R.styleable.LinearLayout_divider)); |
| 227 | mShowDividers = a.getInt(R.styleable.LinearLayout_showDividers, SHOW_DIVIDER_NONE); |
| 228 | mDividerPadding = a.getDimensionPixelSize(R.styleable.LinearLayout_dividerPadding, 0); |
| 229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | a.recycle(); |
| 231 | } |
| 232 | |
| 233 | /** |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 234 | * Set how dividers should be shown between items in this layout |
| 235 | * |
| 236 | * @param showDividers One or more of {@link #SHOW_DIVIDER_BEGINNING}, |
| 237 | * {@link #SHOW_DIVIDER_MIDDLE}, or {@link #SHOW_DIVIDER_END}, |
| 238 | * or {@link #SHOW_DIVIDER_NONE} to show no dividers. |
| 239 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 240 | public void setShowDividers(@DividerMode int showDividers) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 241 | if (showDividers != mShowDividers) { |
| 242 | requestLayout(); |
| 243 | } |
| 244 | mShowDividers = showDividers; |
| 245 | } |
| 246 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 247 | @Override |
| 248 | public boolean shouldDelayChildPressedState() { |
| 249 | return false; |
| 250 | } |
| 251 | |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 252 | /** |
| 253 | * @return A flag set indicating how dividers should be shown around items. |
| 254 | * @see #setShowDividers(int) |
| 255 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 256 | @DividerMode |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 257 | public int getShowDividers() { |
| 258 | return mShowDividers; |
| 259 | } |
| 260 | |
| 261 | /** |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 262 | * @return the divider Drawable that will divide each item. |
| 263 | * |
| 264 | * @see #setDividerDrawable(Drawable) |
| 265 | * |
| 266 | * @attr ref android.R.styleable#LinearLayout_divider |
| 267 | */ |
| 268 | public Drawable getDividerDrawable() { |
| 269 | return mDivider; |
| 270 | } |
| 271 | |
| 272 | /** |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 273 | * Set a drawable to be used as a divider between items. |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 274 | * |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 275 | * @param divider Drawable that will divide each item. |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 276 | * |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 277 | * @see #setShowDividers(int) |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 278 | * |
| 279 | * @attr ref android.R.styleable#LinearLayout_divider |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 280 | */ |
| 281 | public void setDividerDrawable(Drawable divider) { |
| 282 | if (divider == mDivider) { |
| 283 | return; |
| 284 | } |
| 285 | mDivider = divider; |
| 286 | if (divider != null) { |
| 287 | mDividerWidth = divider.getIntrinsicWidth(); |
| 288 | mDividerHeight = divider.getIntrinsicHeight(); |
| 289 | } else { |
| 290 | mDividerWidth = 0; |
| 291 | mDividerHeight = 0; |
| 292 | } |
| 293 | setWillNotDraw(divider == null); |
| 294 | requestLayout(); |
| 295 | } |
| 296 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Set padding displayed on both ends of dividers. |
| 299 | * |
| 300 | * @param padding Padding value in pixels that will be applied to each end |
| 301 | * |
| 302 | * @see #setShowDividers(int) |
| 303 | * @see #setDividerDrawable(Drawable) |
| 304 | * @see #getDividerPadding() |
| 305 | */ |
| 306 | public void setDividerPadding(int padding) { |
| 307 | mDividerPadding = padding; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Get the padding size used to inset dividers in pixels |
| 312 | * |
| 313 | * @see #setShowDividers(int) |
| 314 | * @see #setDividerDrawable(Drawable) |
| 315 | * @see #setDividerPadding(int) |
| 316 | */ |
| 317 | public int getDividerPadding() { |
| 318 | return mDividerPadding; |
| 319 | } |
| 320 | |
Adam Powell | 640a66e | 2011-04-29 10:18:53 -0700 | [diff] [blame] | 321 | /** |
| 322 | * Get the width of the current divider drawable. |
| 323 | * |
| 324 | * @hide Used internally by framework. |
| 325 | */ |
| 326 | public int getDividerWidth() { |
| 327 | return mDividerWidth; |
| 328 | } |
| 329 | |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 330 | @Override |
| 331 | protected void onDraw(Canvas canvas) { |
| 332 | if (mDivider == null) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (mOrientation == VERTICAL) { |
| 337 | drawDividersVertical(canvas); |
| 338 | } else { |
| 339 | drawDividersHorizontal(canvas); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void drawDividersVertical(Canvas canvas) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 344 | final int count = getVirtualChildCount(); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 345 | for (int i = 0; i < count; i++) { |
| 346 | final View child = getVirtualChildAt(i); |
| 347 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 348 | if (child != null && child.getVisibility() != GONE) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 349 | if (hasDividerBeforeChildAt(i)) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 350 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Vladimir Baryshnikov | 20761fc | 2012-01-17 14:59:48 -0800 | [diff] [blame] | 351 | final int top = child.getTop() - lp.topMargin - mDividerHeight; |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 352 | drawHorizontalDivider(canvas, top); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 353 | } |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 357 | if (hasDividerBeforeChildAt(count)) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 358 | final View child = getVirtualChildAt(count - 1); |
| 359 | int bottom = 0; |
| 360 | if (child == null) { |
| 361 | bottom = getHeight() - getPaddingBottom() - mDividerHeight; |
| 362 | } else { |
| 363 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 364 | bottom = child.getBottom() + lp.bottomMargin; |
| 365 | } |
| 366 | drawHorizontalDivider(canvas, bottom); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | void drawDividersHorizontal(Canvas canvas) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 371 | final int count = getVirtualChildCount(); |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 372 | final boolean isLayoutRtl = isLayoutRtl(); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 373 | for (int i = 0; i < count; i++) { |
| 374 | final View child = getVirtualChildAt(i); |
| 375 | |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 376 | if (child != null && child.getVisibility() != GONE) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 377 | if (hasDividerBeforeChildAt(i)) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 378 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 379 | final int position; |
| 380 | if (isLayoutRtl) { |
| 381 | position = child.getRight() + lp.rightMargin; |
| 382 | } else { |
| 383 | position = child.getLeft() - lp.leftMargin - mDividerWidth; |
| 384 | } |
| 385 | drawVerticalDivider(canvas, position); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 386 | } |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 390 | if (hasDividerBeforeChildAt(count)) { |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 391 | final View child = getVirtualChildAt(count - 1); |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 392 | int position; |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 393 | if (child == null) { |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 394 | if (isLayoutRtl) { |
| 395 | position = getPaddingLeft(); |
| 396 | } else { |
| 397 | position = getWidth() - getPaddingRight() - mDividerWidth; |
| 398 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 399 | } else { |
| 400 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 401 | if (isLayoutRtl) { |
| 402 | position = child.getLeft() - lp.leftMargin - mDividerWidth; |
| 403 | } else { |
| 404 | position = child.getRight() + lp.rightMargin; |
| 405 | } |
Adam Powell | 35aecd5 | 2011-07-01 13:43:49 -0700 | [diff] [blame] | 406 | } |
Fabrice Di Meglio | 4ab6968 | 2012-07-02 19:12:40 -0700 | [diff] [blame] | 407 | drawVerticalDivider(canvas, position); |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
| 411 | void drawHorizontalDivider(Canvas canvas, int top) { |
| 412 | mDivider.setBounds(getPaddingLeft() + mDividerPadding, top, |
| 413 | getWidth() - getPaddingRight() - mDividerPadding, top + mDividerHeight); |
| 414 | mDivider.draw(canvas); |
| 415 | } |
| 416 | |
| 417 | void drawVerticalDivider(Canvas canvas, int left) { |
| 418 | mDivider.setBounds(left, getPaddingTop() + mDividerPadding, |
| 419 | left + mDividerWidth, getHeight() - getPaddingBottom() - mDividerPadding); |
| 420 | mDivider.draw(canvas); |
| 421 | } |
| 422 | |
| 423 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | * <p>Indicates whether widgets contained within this layout are aligned |
| 425 | * on their baseline or not.</p> |
| 426 | * |
| 427 | * @return true when widgets are baseline-aligned, false otherwise |
| 428 | */ |
| 429 | public boolean isBaselineAligned() { |
| 430 | return mBaselineAligned; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * <p>Defines whether widgets contained in this layout are |
| 435 | * baseline-aligned or not.</p> |
| 436 | * |
| 437 | * @param baselineAligned true to align widgets on their baseline, |
| 438 | * false otherwise |
| 439 | * |
| 440 | * @attr ref android.R.styleable#LinearLayout_baselineAligned |
| 441 | */ |
| 442 | @android.view.RemotableViewMethod |
| 443 | public void setBaselineAligned(boolean baselineAligned) { |
| 444 | mBaselineAligned = baselineAligned; |
| 445 | } |
| 446 | |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 447 | /** |
| 448 | * When true, all children with a weight will be considered having |
| 449 | * the minimum size of the largest child. If false, all children are |
| 450 | * measured normally. |
| 451 | * |
| 452 | * @return True to measure children with a weight using the minimum |
| 453 | * size of the largest child, false otherwise. |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 454 | * |
| 455 | * @attr ref android.R.styleable#LinearLayout_measureWithLargestChild |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 456 | */ |
| 457 | public boolean isMeasureWithLargestChildEnabled() { |
| 458 | return mUseLargestChild; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * When set to true, all children with a weight will be considered having |
| 463 | * the minimum size of the largest child. If false, all children are |
| 464 | * measured normally. |
| 465 | * |
| 466 | * Disabled by default. |
| 467 | * |
| 468 | * @param enabled True to measure children with a weight using the |
| 469 | * minimum size of the largest child, false otherwise. |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 470 | * |
| 471 | * @attr ref android.R.styleable#LinearLayout_measureWithLargestChild |
Romain Guy | 9295ada | 2010-06-15 11:33:24 -0700 | [diff] [blame] | 472 | */ |
| 473 | @android.view.RemotableViewMethod |
| 474 | public void setMeasureWithLargestChildEnabled(boolean enabled) { |
| 475 | mUseLargestChild = enabled; |
| 476 | } |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | @Override |
| 479 | public int getBaseline() { |
| 480 | if (mBaselineAlignedChildIndex < 0) { |
| 481 | return super.getBaseline(); |
| 482 | } |
| 483 | |
| 484 | if (getChildCount() <= mBaselineAlignedChildIndex) { |
| 485 | throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " |
| 486 | + "set to an index that is out of bounds."); |
| 487 | } |
| 488 | |
| 489 | final View child = getChildAt(mBaselineAlignedChildIndex); |
| 490 | final int childBaseline = child.getBaseline(); |
| 491 | |
| 492 | if (childBaseline == -1) { |
| 493 | if (mBaselineAlignedChildIndex == 0) { |
| 494 | // this is just the default case, safe to return -1 |
| 495 | return -1; |
| 496 | } |
| 497 | // the user picked an index that points to something that doesn't |
| 498 | // know how to calculate its baseline. |
| 499 | throw new RuntimeException("mBaselineAlignedChildIndex of LinearLayout " |
| 500 | + "points to a View that doesn't know how to get its baseline."); |
| 501 | } |
| 502 | |
| 503 | // TODO: This should try to take into account the virtual offsets |
| 504 | // (See getNextLocationOffset and getLocationOffset) |
| 505 | // We should add to childTop: |
| 506 | // sum([getNextLocationOffset(getChildAt(i)) / i < mBaselineAlignedChildIndex]) |
| 507 | // and also add: |
| 508 | // getLocationOffset(child) |
| 509 | int childTop = mBaselineChildTop; |
| 510 | |
| 511 | if (mOrientation == VERTICAL) { |
| 512 | final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 513 | if (majorGravity != Gravity.TOP) { |
| 514 | switch (majorGravity) { |
| 515 | case Gravity.BOTTOM: |
| 516 | childTop = mBottom - mTop - mPaddingBottom - mTotalLength; |
| 517 | break; |
| 518 | |
| 519 | case Gravity.CENTER_VERTICAL: |
| 520 | childTop += ((mBottom - mTop - mPaddingTop - mPaddingBottom) - |
| 521 | mTotalLength) / 2; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 528 | return childTop + lp.topMargin + childBaseline; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * @return The index of the child that will be used if this layout is |
| 533 | * part of a larger layout that is baseline aligned, or -1 if none has |
| 534 | * been set. |
| 535 | */ |
| 536 | public int getBaselineAlignedChildIndex() { |
| 537 | return mBaselineAlignedChildIndex; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * @param i The index of the child that will be used if this layout is |
| 542 | * part of a larger layout that is baseline aligned. |
| 543 | * |
| 544 | * @attr ref android.R.styleable#LinearLayout_baselineAlignedChildIndex |
| 545 | */ |
| 546 | @android.view.RemotableViewMethod |
| 547 | public void setBaselineAlignedChildIndex(int i) { |
| 548 | if ((i < 0) || (i >= getChildCount())) { |
| 549 | throw new IllegalArgumentException("base aligned child index out " |
| 550 | + "of range (0, " + getChildCount() + ")"); |
| 551 | } |
| 552 | mBaselineAlignedChildIndex = i; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * <p>Returns the view at the specified index. This method can be overriden |
| 557 | * to take into account virtual children. Refer to |
| 558 | * {@link android.widget.TableLayout} and {@link android.widget.TableRow} |
| 559 | * for an example.</p> |
| 560 | * |
| 561 | * @param index the child's index |
| 562 | * @return the child at the specified index |
| 563 | */ |
| 564 | View getVirtualChildAt(int index) { |
| 565 | return getChildAt(index); |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * <p>Returns the virtual number of children. This number might be different |
| 570 | * than the actual number of children if the layout can hold virtual |
| 571 | * children. Refer to |
| 572 | * {@link android.widget.TableLayout} and {@link android.widget.TableRow} |
| 573 | * for an example.</p> |
| 574 | * |
| 575 | * @return the virtual number of children |
| 576 | */ |
| 577 | int getVirtualChildCount() { |
| 578 | return getChildCount(); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Returns the desired weights sum. |
| 583 | * |
| 584 | * @return A number greater than 0.0f if the weight sum is defined, or |
| 585 | * a number lower than or equals to 0.0f if not weight sum is |
| 586 | * to be used. |
| 587 | */ |
| 588 | public float getWeightSum() { |
| 589 | return mWeightSum; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Defines the desired weights sum. If unspecified the weights sum is computed |
| 594 | * at layout time by adding the layout_weight of each child. |
| 595 | * |
| 596 | * This can be used for instance to give a single child 50% of the total |
| 597 | * available space by giving it a layout_weight of 0.5 and setting the |
| 598 | * weightSum to 1.0. |
| 599 | * |
| 600 | * @param weightSum a number greater than 0.0f, or a number lower than or equals |
| 601 | * to 0.0f if the weight sum should be computed from the children's |
| 602 | * layout_weight |
| 603 | */ |
| 604 | @android.view.RemotableViewMethod |
| 605 | public void setWeightSum(float weightSum) { |
| 606 | mWeightSum = Math.max(0.0f, weightSum); |
| 607 | } |
| 608 | |
| 609 | @Override |
| 610 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 611 | if (mOrientation == VERTICAL) { |
| 612 | measureVertical(widthMeasureSpec, heightMeasureSpec); |
| 613 | } else { |
| 614 | measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /** |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 619 | * Determines where to position dividers between children. |
| 620 | * |
| 621 | * @param childIndex Index of child to check for preceding divider |
| 622 | * @return true if there should be a divider before the child at childIndex |
| 623 | * @hide Pending API consideration. Currently only used internally by the system. |
| 624 | */ |
| 625 | protected boolean hasDividerBeforeChildAt(int childIndex) { |
| 626 | if (childIndex == 0) { |
| 627 | return (mShowDividers & SHOW_DIVIDER_BEGINNING) != 0; |
| 628 | } else if (childIndex == getChildCount()) { |
| 629 | return (mShowDividers & SHOW_DIVIDER_END) != 0; |
Adam Powell | bf68f83 | 2011-06-20 18:23:30 -0700 | [diff] [blame] | 630 | } else if ((mShowDividers & SHOW_DIVIDER_MIDDLE) != 0) { |
| 631 | boolean hasVisibleViewBefore = false; |
| 632 | for (int i = childIndex - 1; i >= 0; i--) { |
| 633 | if (getChildAt(i).getVisibility() != GONE) { |
| 634 | hasVisibleViewBefore = true; |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | return hasVisibleViewBefore; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 639 | } |
Adam Powell | bf68f83 | 2011-06-20 18:23:30 -0700 | [diff] [blame] | 640 | return false; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | * Measures the children when the orientation of this LinearLayout is set |
| 645 | * to {@link #VERTICAL}. |
| 646 | * |
| 647 | * @param widthMeasureSpec Horizontal space requirements as imposed by the parent. |
| 648 | * @param heightMeasureSpec Vertical space requirements as imposed by the parent. |
| 649 | * |
| 650 | * @see #getOrientation() |
| 651 | * @see #setOrientation(int) |
| 652 | * @see #onMeasure(int, int) |
| 653 | */ |
| 654 | void measureVertical(int widthMeasureSpec, int heightMeasureSpec) { |
| 655 | mTotalLength = 0; |
| 656 | int maxWidth = 0; |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 657 | int childState = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 658 | int alternativeMaxWidth = 0; |
| 659 | int weightedMaxWidth = 0; |
| 660 | boolean allFillParent = true; |
| 661 | float totalWeight = 0; |
| 662 | |
| 663 | final int count = getVirtualChildCount(); |
| 664 | |
| 665 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 666 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 667 | |
| 668 | boolean matchWidth = false; |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 669 | boolean skippedMeasure = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | |
| 671 | final int baselineChildIndex = mBaselineAlignedChildIndex; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 672 | final boolean useLargestChild = mUseLargestChild; |
| 673 | |
| 674 | int largestChildHeight = Integer.MIN_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | |
| 676 | // See how tall everyone is. Also remember max width. |
| 677 | for (int i = 0; i < count; ++i) { |
| 678 | final View child = getVirtualChildAt(i); |
| 679 | |
| 680 | if (child == null) { |
| 681 | mTotalLength += measureNullChild(i); |
| 682 | continue; |
| 683 | } |
| 684 | |
| 685 | if (child.getVisibility() == View.GONE) { |
| 686 | i += getChildrenSkipCount(child, i); |
| 687 | continue; |
| 688 | } |
| 689 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 690 | if (hasDividerBeforeChildAt(i)) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 691 | mTotalLength += mDividerHeight; |
| 692 | } |
| 693 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 694 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 695 | |
| 696 | totalWeight += lp.weight; |
| 697 | |
| 698 | if (heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0) { |
| 699 | // Optimization: don't bother measuring children who are going to use |
| 700 | // leftover space. These views will get measured again down below if |
| 701 | // there is any leftover space. |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 702 | final int totalLength = mTotalLength; |
| 703 | mTotalLength = Math.max(totalLength, totalLength + lp.topMargin + lp.bottomMargin); |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 704 | skippedMeasure = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } else { |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 706 | int oldHeight = Integer.MIN_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 708 | if (lp.height == 0 && lp.weight > 0) { |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 709 | // heightMode is either UNSPECIFIED or AT_MOST, and this |
| 710 | // child wanted to stretch to fill available space. |
| 711 | // Translate that to WRAP_CONTENT so that it does not end up |
| 712 | // with a height of 0 |
| 713 | oldHeight = 0; |
| 714 | lp.height = LayoutParams.WRAP_CONTENT; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 715 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 717 | // Determine how big this child would like to be. If this or |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 718 | // previous children have given a weight, then we allow it to |
| 719 | // use all available space (and we will shrink things later |
| 720 | // if needed). |
| 721 | measureChildBeforeLayout( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | child, i, widthMeasureSpec, 0, heightMeasureSpec, |
| 723 | totalWeight == 0 ? mTotalLength : 0); |
| 724 | |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 725 | if (oldHeight != Integer.MIN_VALUE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | lp.height = oldHeight; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 727 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 729 | final int childHeight = child.getMeasuredHeight(); |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 730 | final int totalLength = mTotalLength; |
| 731 | mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin + |
| 732 | lp.bottomMargin + getNextLocationOffset(child)); |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 733 | |
| 734 | if (useLargestChild) { |
| 735 | largestChildHeight = Math.max(childHeight, largestChildHeight); |
| 736 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /** |
| 740 | * If applicable, compute the additional offset to the child's baseline |
| 741 | * we'll need later when asked {@link #getBaseline}. |
| 742 | */ |
| 743 | if ((baselineChildIndex >= 0) && (baselineChildIndex == i + 1)) { |
| 744 | mBaselineChildTop = mTotalLength; |
| 745 | } |
| 746 | |
| 747 | // if we are trying to use a child index for our baseline, the above |
| 748 | // book keeping only works if there are no children above it with |
| 749 | // weight. fail fast to aid the developer. |
| 750 | if (i < baselineChildIndex && lp.weight > 0) { |
| 751 | throw new RuntimeException("A child of LinearLayout with index " |
| 752 | + "less than mBaselineAlignedChildIndex has weight > 0, which " |
| 753 | + "won't work. Either remove the weight, or don't set " |
| 754 | + "mBaselineAlignedChildIndex."); |
| 755 | } |
| 756 | |
| 757 | boolean matchWidthLocally = false; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 758 | if (widthMode != MeasureSpec.EXACTLY && lp.width == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | // The width of the linear layout will scale, and at least one |
| 760 | // child said it wanted to match our width. Set a flag |
| 761 | // indicating that we need to remeasure at least that view when |
| 762 | // we know our width. |
| 763 | matchWidth = true; |
| 764 | matchWidthLocally = true; |
| 765 | } |
| 766 | |
| 767 | final int margin = lp.leftMargin + lp.rightMargin; |
| 768 | final int measuredWidth = child.getMeasuredWidth() + margin; |
| 769 | maxWidth = Math.max(maxWidth, measuredWidth); |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 770 | childState = combineMeasuredStates(childState, child.getMeasuredState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 772 | allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | if (lp.weight > 0) { |
| 774 | /* |
| 775 | * Widths of weighted Views are bogus if we end up |
| 776 | * remeasuring, so keep them separate. |
| 777 | */ |
| 778 | weightedMaxWidth = Math.max(weightedMaxWidth, |
| 779 | matchWidthLocally ? margin : measuredWidth); |
| 780 | } else { |
| 781 | alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 782 | matchWidthLocally ? margin : measuredWidth); |
| 783 | } |
| 784 | |
| 785 | i += getChildrenSkipCount(child, i); |
| 786 | } |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 787 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 788 | if (mTotalLength > 0 && hasDividerBeforeChildAt(count)) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 789 | mTotalLength += mDividerHeight; |
| 790 | } |
| 791 | |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 792 | if (useLargestChild && |
| 793 | (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED)) { |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 794 | mTotalLength = 0; |
| 795 | |
| 796 | for (int i = 0; i < count; ++i) { |
| 797 | final View child = getVirtualChildAt(i); |
| 798 | |
| 799 | if (child == null) { |
| 800 | mTotalLength += measureNullChild(i); |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | if (child.getVisibility() == GONE) { |
| 805 | i += getChildrenSkipCount(child, i); |
| 806 | continue; |
| 807 | } |
| 808 | |
| 809 | final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 810 | child.getLayoutParams(); |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 811 | // Account for negative margins |
| 812 | final int totalLength = mTotalLength; |
| 813 | mTotalLength = Math.max(totalLength, totalLength + largestChildHeight + |
| 814 | lp.topMargin + lp.bottomMargin + getNextLocationOffset(child)); |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | // Add in our padding |
| 819 | mTotalLength += mPaddingTop + mPaddingBottom; |
| 820 | |
| 821 | int heightSize = mTotalLength; |
| 822 | |
| 823 | // Check against our minimum height |
| 824 | heightSize = Math.max(heightSize, getSuggestedMinimumHeight()); |
| 825 | |
| 826 | // Reconcile our calculated size with the heightMeasureSpec |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 827 | int heightSizeAndState = resolveSizeAndState(heightSize, heightMeasureSpec, 0); |
| 828 | heightSize = heightSizeAndState & MEASURED_SIZE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | |
| 830 | // Either expand children with weight to take up available space or |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 831 | // shrink them if they extend beyond our current bounds. If we skipped |
| 832 | // measurement on any children, we need to measure them now. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | int delta = heightSize - mTotalLength; |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 834 | if (skippedMeasure || delta != 0 && totalWeight > 0.0f) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; |
| 836 | |
| 837 | mTotalLength = 0; |
| 838 | |
| 839 | for (int i = 0; i < count; ++i) { |
| 840 | final View child = getVirtualChildAt(i); |
| 841 | |
| 842 | if (child.getVisibility() == View.GONE) { |
| 843 | continue; |
| 844 | } |
| 845 | |
| 846 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 847 | |
| 848 | float childExtra = lp.weight; |
| 849 | if (childExtra > 0) { |
| 850 | // Child said it could absorb extra space -- give him his share |
| 851 | int share = (int) (childExtra * delta / weightSum); |
| 852 | weightSum -= childExtra; |
| 853 | delta -= share; |
| 854 | |
| 855 | final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, |
| 856 | mPaddingLeft + mPaddingRight + |
| 857 | lp.leftMargin + lp.rightMargin, lp.width); |
| 858 | |
| 859 | // TODO: Use a field like lp.isMeasured to figure out if this |
| 860 | // child has been previously measured |
| 861 | if ((lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)) { |
| 862 | // child was measured once already above... |
| 863 | // base new measurement on stored values |
| 864 | int childHeight = child.getMeasuredHeight() + share; |
| 865 | if (childHeight < 0) { |
| 866 | childHeight = 0; |
| 867 | } |
| 868 | |
| 869 | child.measure(childWidthMeasureSpec, |
| 870 | MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)); |
| 871 | } else { |
| 872 | // child was skipped in the loop above. |
| 873 | // Measure for this first time here |
| 874 | child.measure(childWidthMeasureSpec, |
| 875 | MeasureSpec.makeMeasureSpec(share > 0 ? share : 0, |
| 876 | MeasureSpec.EXACTLY)); |
| 877 | } |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 878 | |
| 879 | // Child may now not fit in vertical dimension. |
| 880 | childState = combineMeasuredStates(childState, child.getMeasuredState() |
| 881 | & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | final int margin = lp.leftMargin + lp.rightMargin; |
| 885 | final int measuredWidth = child.getMeasuredWidth() + margin; |
| 886 | maxWidth = Math.max(maxWidth, measuredWidth); |
| 887 | |
| 888 | boolean matchWidthLocally = widthMode != MeasureSpec.EXACTLY && |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 889 | lp.width == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | |
| 891 | alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 892 | matchWidthLocally ? margin : measuredWidth); |
| 893 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 894 | allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 896 | final int totalLength = mTotalLength; |
| 897 | mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredHeight() + |
| 898 | lp.topMargin + lp.bottomMargin + getNextLocationOffset(child)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | // Add in our padding |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 902 | mTotalLength += mPaddingTop + mPaddingBottom; |
| 903 | // TODO: Should we recompute the heightSpec based on the new total length? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | } else { |
| 905 | alternativeMaxWidth = Math.max(alternativeMaxWidth, |
| 906 | weightedMaxWidth); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 907 | |
| 908 | |
| 909 | // We have no limit, so make all weighted views as tall as the largest child. |
| 910 | // Children will have already been measured once. |
Adam Powell | eabc919 | 2012-05-04 14:49:46 -0700 | [diff] [blame] | 911 | if (useLargestChild && heightMode != MeasureSpec.EXACTLY) { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 912 | for (int i = 0; i < count; i++) { |
| 913 | final View child = getVirtualChildAt(i); |
| 914 | |
| 915 | if (child == null || child.getVisibility() == View.GONE) { |
| 916 | continue; |
| 917 | } |
| 918 | |
| 919 | final LinearLayout.LayoutParams lp = |
| 920 | (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 921 | |
| 922 | float childExtra = lp.weight; |
| 923 | if (childExtra > 0) { |
| 924 | child.measure( |
| 925 | MeasureSpec.makeMeasureSpec(child.getMeasuredWidth(), |
| 926 | MeasureSpec.EXACTLY), |
| 927 | MeasureSpec.makeMeasureSpec(largestChildHeight, |
| 928 | MeasureSpec.EXACTLY)); |
| 929 | } |
| 930 | } |
| 931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | if (!allFillParent && widthMode != MeasureSpec.EXACTLY) { |
| 935 | maxWidth = alternativeMaxWidth; |
| 936 | } |
| 937 | |
| 938 | maxWidth += mPaddingLeft + mPaddingRight; |
| 939 | |
| 940 | // Check against our minimum width |
| 941 | maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 942 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 943 | setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), |
| 944 | heightSizeAndState); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | |
| 946 | if (matchWidth) { |
| 947 | forceUniformWidth(count, heightMeasureSpec); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | private void forceUniformWidth(int count, int heightMeasureSpec) { |
| 952 | // Pretend that the linear layout has an exact size. |
| 953 | int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), |
| 954 | MeasureSpec.EXACTLY); |
| 955 | for (int i = 0; i< count; ++i) { |
| 956 | final View child = getVirtualChildAt(i); |
| 957 | if (child.getVisibility() != GONE) { |
| 958 | LinearLayout.LayoutParams lp = ((LinearLayout.LayoutParams)child.getLayoutParams()); |
| 959 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 960 | if (lp.width == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | // Temporarily force children to reuse their old measured height |
| 962 | // FIXME: this may not be right for something like wrapping text? |
| 963 | int oldHeight = lp.height; |
| 964 | lp.height = child.getMeasuredHeight(); |
| 965 | |
| 966 | // Remeasue with new dimensions |
| 967 | measureChildWithMargins(child, uniformMeasureSpec, 0, heightMeasureSpec, 0); |
| 968 | lp.height = oldHeight; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Measures the children when the orientation of this LinearLayout is set |
| 976 | * to {@link #HORIZONTAL}. |
| 977 | * |
| 978 | * @param widthMeasureSpec Horizontal space requirements as imposed by the parent. |
| 979 | * @param heightMeasureSpec Vertical space requirements as imposed by the parent. |
| 980 | * |
| 981 | * @see #getOrientation() |
| 982 | * @see #setOrientation(int) |
| 983 | * @see #onMeasure(int, int) |
| 984 | */ |
| 985 | void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { |
| 986 | mTotalLength = 0; |
| 987 | int maxHeight = 0; |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 988 | int childState = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | int alternativeMaxHeight = 0; |
| 990 | int weightedMaxHeight = 0; |
| 991 | boolean allFillParent = true; |
| 992 | float totalWeight = 0; |
| 993 | |
| 994 | final int count = getVirtualChildCount(); |
| 995 | |
| 996 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 997 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 998 | |
| 999 | boolean matchHeight = false; |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 1000 | boolean skippedMeasure = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | |
| 1002 | if (mMaxAscent == null || mMaxDescent == null) { |
| 1003 | mMaxAscent = new int[VERTICAL_GRAVITY_COUNT]; |
| 1004 | mMaxDescent = new int[VERTICAL_GRAVITY_COUNT]; |
| 1005 | } |
| 1006 | |
| 1007 | final int[] maxAscent = mMaxAscent; |
| 1008 | final int[] maxDescent = mMaxDescent; |
| 1009 | |
| 1010 | maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; |
| 1011 | maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; |
| 1012 | |
| 1013 | final boolean baselineAligned = mBaselineAligned; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1014 | final boolean useLargestChild = mUseLargestChild; |
Romain Guy | c352090 | 2010-02-25 11:01:01 -0800 | [diff] [blame] | 1015 | |
| 1016 | final boolean isExactly = widthMode == MeasureSpec.EXACTLY; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1017 | |
| 1018 | int largestChildWidth = Integer.MIN_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1019 | |
| 1020 | // See how wide everyone is. Also remember max height. |
| 1021 | for (int i = 0; i < count; ++i) { |
| 1022 | final View child = getVirtualChildAt(i); |
| 1023 | |
| 1024 | if (child == null) { |
| 1025 | mTotalLength += measureNullChild(i); |
| 1026 | continue; |
| 1027 | } |
| 1028 | |
| 1029 | if (child.getVisibility() == GONE) { |
| 1030 | i += getChildrenSkipCount(child, i); |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1034 | if (hasDividerBeforeChildAt(i)) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 1035 | mTotalLength += mDividerWidth; |
| 1036 | } |
| 1037 | |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1038 | final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 1039 | child.getLayoutParams(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | |
| 1041 | totalWeight += lp.weight; |
| 1042 | |
| 1043 | if (widthMode == MeasureSpec.EXACTLY && lp.width == 0 && lp.weight > 0) { |
| 1044 | // Optimization: don't bother measuring children who are going to use |
| 1045 | // leftover space. These views will get measured again down below if |
| 1046 | // there is any leftover space. |
Romain Guy | c352090 | 2010-02-25 11:01:01 -0800 | [diff] [blame] | 1047 | if (isExactly) { |
| 1048 | mTotalLength += lp.leftMargin + lp.rightMargin; |
| 1049 | } else { |
| 1050 | final int totalLength = mTotalLength; |
| 1051 | mTotalLength = Math.max(totalLength, totalLength + |
| 1052 | lp.leftMargin + lp.rightMargin); |
| 1053 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | |
| 1055 | // Baseline alignment requires to measure widgets to obtain the |
Romain Guy | 6c5664a | 2010-02-24 18:55:22 -0800 | [diff] [blame] | 1056 | // baseline offset (in particular for TextViews). The following |
| 1057 | // defeats the optimization mentioned above. Allow the child to |
| 1058 | // use as much space as it wants because we can shrink things |
| 1059 | // later (and re-measure). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | if (baselineAligned) { |
| 1061 | final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 1062 | child.measure(freeSpec, freeSpec); |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 1063 | } else { |
| 1064 | skippedMeasure = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | } |
| 1066 | } else { |
| 1067 | int oldWidth = Integer.MIN_VALUE; |
| 1068 | |
| 1069 | if (lp.width == 0 && lp.weight > 0) { |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 1070 | // widthMode is either UNSPECIFIED or AT_MOST, and this |
| 1071 | // child |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | // wanted to stretch to fill available space. Translate that to |
| 1073 | // WRAP_CONTENT so that it does not end up with a width of 0 |
| 1074 | oldWidth = 0; |
| 1075 | lp.width = LayoutParams.WRAP_CONTENT; |
| 1076 | } |
| 1077 | |
| 1078 | // Determine how big this child would like to be. If this or |
| 1079 | // previous children have given a weight, then we allow it to |
| 1080 | // use all available space (and we will shrink things later |
| 1081 | // if needed). |
| 1082 | measureChildBeforeLayout(child, i, widthMeasureSpec, |
| 1083 | totalWeight == 0 ? mTotalLength : 0, |
| 1084 | heightMeasureSpec, 0); |
| 1085 | |
| 1086 | if (oldWidth != Integer.MIN_VALUE) { |
| 1087 | lp.width = oldWidth; |
| 1088 | } |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1089 | |
| 1090 | final int childWidth = child.getMeasuredWidth(); |
Romain Guy | c352090 | 2010-02-25 11:01:01 -0800 | [diff] [blame] | 1091 | if (isExactly) { |
| 1092 | mTotalLength += childWidth + lp.leftMargin + lp.rightMargin + |
| 1093 | getNextLocationOffset(child); |
| 1094 | } else { |
| 1095 | final int totalLength = mTotalLength; |
| 1096 | mTotalLength = Math.max(totalLength, totalLength + childWidth + lp.leftMargin + |
| 1097 | lp.rightMargin + getNextLocationOffset(child)); |
| 1098 | } |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1099 | |
| 1100 | if (useLargestChild) { |
| 1101 | largestChildWidth = Math.max(childWidth, largestChildWidth); |
| 1102 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | boolean matchHeightLocally = false; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1106 | if (heightMode != MeasureSpec.EXACTLY && lp.height == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | // The height of the linear layout will scale, and at least one |
| 1108 | // child said it wanted to match our height. Set a flag indicating that |
| 1109 | // we need to remeasure at least that view when we know our height. |
| 1110 | matchHeight = true; |
| 1111 | matchHeightLocally = true; |
| 1112 | } |
| 1113 | |
| 1114 | final int margin = lp.topMargin + lp.bottomMargin; |
| 1115 | final int childHeight = child.getMeasuredHeight() + margin; |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1116 | childState = combineMeasuredStates(childState, child.getMeasuredState()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | |
| 1118 | if (baselineAligned) { |
| 1119 | final int childBaseline = child.getBaseline(); |
| 1120 | if (childBaseline != -1) { |
| 1121 | // Translates the child's vertical gravity into an index |
| 1122 | // in the range 0..VERTICAL_GRAVITY_COUNT |
| 1123 | final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) |
| 1124 | & Gravity.VERTICAL_GRAVITY_MASK; |
| 1125 | final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) |
| 1126 | & ~Gravity.AXIS_SPECIFIED) >> 1; |
| 1127 | |
| 1128 | maxAscent[index] = Math.max(maxAscent[index], childBaseline); |
| 1129 | maxDescent[index] = Math.max(maxDescent[index], childHeight - childBaseline); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | maxHeight = Math.max(maxHeight, childHeight); |
| 1134 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1135 | allFillParent = allFillParent && lp.height == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | if (lp.weight > 0) { |
| 1137 | /* |
| 1138 | * Heights of weighted Views are bogus if we end up |
| 1139 | * remeasuring, so keep them separate. |
| 1140 | */ |
| 1141 | weightedMaxHeight = Math.max(weightedMaxHeight, |
| 1142 | matchHeightLocally ? margin : childHeight); |
| 1143 | } else { |
| 1144 | alternativeMaxHeight = Math.max(alternativeMaxHeight, |
| 1145 | matchHeightLocally ? margin : childHeight); |
| 1146 | } |
| 1147 | |
| 1148 | i += getChildrenSkipCount(child, i); |
| 1149 | } |
| 1150 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1151 | if (mTotalLength > 0 && hasDividerBeforeChildAt(count)) { |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 1152 | mTotalLength += mDividerWidth; |
| 1153 | } |
| 1154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1155 | // Check mMaxAscent[INDEX_TOP] first because it maps to Gravity.TOP, |
| 1156 | // the most common case |
| 1157 | if (maxAscent[INDEX_TOP] != -1 || |
| 1158 | maxAscent[INDEX_CENTER_VERTICAL] != -1 || |
| 1159 | maxAscent[INDEX_BOTTOM] != -1 || |
| 1160 | maxAscent[INDEX_FILL] != -1) { |
| 1161 | final int ascent = Math.max(maxAscent[INDEX_FILL], |
| 1162 | Math.max(maxAscent[INDEX_CENTER_VERTICAL], |
| 1163 | Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); |
| 1164 | final int descent = Math.max(maxDescent[INDEX_FILL], |
| 1165 | Math.max(maxDescent[INDEX_CENTER_VERTICAL], |
| 1166 | Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); |
| 1167 | maxHeight = Math.max(maxHeight, ascent + descent); |
| 1168 | } |
| 1169 | |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1170 | if (useLargestChild && |
| 1171 | (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED)) { |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1172 | mTotalLength = 0; |
| 1173 | |
| 1174 | for (int i = 0; i < count; ++i) { |
| 1175 | final View child = getVirtualChildAt(i); |
| 1176 | |
| 1177 | if (child == null) { |
| 1178 | mTotalLength += measureNullChild(i); |
| 1179 | continue; |
| 1180 | } |
| 1181 | |
| 1182 | if (child.getVisibility() == GONE) { |
| 1183 | i += getChildrenSkipCount(child, i); |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) |
| 1188 | child.getLayoutParams(); |
Romain Guy | c352090 | 2010-02-25 11:01:01 -0800 | [diff] [blame] | 1189 | if (isExactly) { |
| 1190 | mTotalLength += largestChildWidth + lp.leftMargin + lp.rightMargin + |
| 1191 | getNextLocationOffset(child); |
| 1192 | } else { |
| 1193 | final int totalLength = mTotalLength; |
| 1194 | mTotalLength = Math.max(totalLength, totalLength + largestChildWidth + |
| 1195 | lp.leftMargin + lp.rightMargin + getNextLocationOffset(child)); |
| 1196 | } |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | // Add in our padding |
| 1201 | mTotalLength += mPaddingLeft + mPaddingRight; |
| 1202 | |
| 1203 | int widthSize = mTotalLength; |
| 1204 | |
| 1205 | // Check against our minimum width |
| 1206 | widthSize = Math.max(widthSize, getSuggestedMinimumWidth()); |
| 1207 | |
| 1208 | // Reconcile our calculated size with the widthMeasureSpec |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1209 | int widthSizeAndState = resolveSizeAndState(widthSize, widthMeasureSpec, 0); |
| 1210 | widthSize = widthSizeAndState & MEASURED_SIZE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | |
| 1212 | // Either expand children with weight to take up available space or |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 1213 | // shrink them if they extend beyond our current bounds. If we skipped |
| 1214 | // measurement on any children, we need to measure them now. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | int delta = widthSize - mTotalLength; |
Alan Viverette | 65be9cc | 2014-02-24 18:21:09 -0800 | [diff] [blame] | 1216 | if (skippedMeasure || delta != 0 && totalWeight > 0.0f) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; |
| 1218 | |
| 1219 | maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; |
| 1220 | maxDescent[0] = maxDescent[1] = maxDescent[2] = maxDescent[3] = -1; |
| 1221 | maxHeight = -1; |
| 1222 | |
| 1223 | mTotalLength = 0; |
| 1224 | |
| 1225 | for (int i = 0; i < count; ++i) { |
| 1226 | final View child = getVirtualChildAt(i); |
| 1227 | |
| 1228 | if (child == null || child.getVisibility() == View.GONE) { |
| 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | final LinearLayout.LayoutParams lp = |
| 1233 | (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1234 | |
| 1235 | float childExtra = lp.weight; |
| 1236 | if (childExtra > 0) { |
| 1237 | // Child said it could absorb extra space -- give him his share |
| 1238 | int share = (int) (childExtra * delta / weightSum); |
| 1239 | weightSum -= childExtra; |
| 1240 | delta -= share; |
| 1241 | |
| 1242 | final int childHeightMeasureSpec = getChildMeasureSpec( |
| 1243 | heightMeasureSpec, |
| 1244 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin, |
| 1245 | lp.height); |
| 1246 | |
| 1247 | // TODO: Use a field like lp.isMeasured to figure out if this |
| 1248 | // child has been previously measured |
| 1249 | if ((lp.width != 0) || (widthMode != MeasureSpec.EXACTLY)) { |
| 1250 | // child was measured once already above ... base new measurement |
| 1251 | // on stored values |
| 1252 | int childWidth = child.getMeasuredWidth() + share; |
| 1253 | if (childWidth < 0) { |
| 1254 | childWidth = 0; |
| 1255 | } |
| 1256 | |
| 1257 | child.measure( |
| 1258 | MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), |
| 1259 | childHeightMeasureSpec); |
| 1260 | } else { |
| 1261 | // child was skipped in the loop above. Measure for this first time here |
| 1262 | child.measure(MeasureSpec.makeMeasureSpec( |
| 1263 | share > 0 ? share : 0, MeasureSpec.EXACTLY), |
| 1264 | childHeightMeasureSpec); |
| 1265 | } |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1266 | |
| 1267 | // Child may now not fit in horizontal dimension. |
| 1268 | childState = combineMeasuredStates(childState, |
| 1269 | child.getMeasuredState() & MEASURED_STATE_MASK); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
Romain Guy | c352090 | 2010-02-25 11:01:01 -0800 | [diff] [blame] | 1272 | if (isExactly) { |
| 1273 | mTotalLength += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin + |
| 1274 | getNextLocationOffset(child); |
| 1275 | } else { |
| 1276 | final int totalLength = mTotalLength; |
| 1277 | mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredWidth() + |
| 1278 | lp.leftMargin + lp.rightMargin + getNextLocationOffset(child)); |
| 1279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | |
| 1281 | boolean matchHeightLocally = heightMode != MeasureSpec.EXACTLY && |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1282 | lp.height == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | |
| 1284 | final int margin = lp.topMargin + lp .bottomMargin; |
| 1285 | int childHeight = child.getMeasuredHeight() + margin; |
| 1286 | maxHeight = Math.max(maxHeight, childHeight); |
| 1287 | alternativeMaxHeight = Math.max(alternativeMaxHeight, |
| 1288 | matchHeightLocally ? margin : childHeight); |
| 1289 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1290 | allFillParent = allFillParent && lp.height == LayoutParams.MATCH_PARENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | |
| 1292 | if (baselineAligned) { |
| 1293 | final int childBaseline = child.getBaseline(); |
| 1294 | if (childBaseline != -1) { |
| 1295 | // Translates the child's vertical gravity into an index in the range 0..2 |
| 1296 | final int gravity = (lp.gravity < 0 ? mGravity : lp.gravity) |
| 1297 | & Gravity.VERTICAL_GRAVITY_MASK; |
| 1298 | final int index = ((gravity >> Gravity.AXIS_Y_SHIFT) |
| 1299 | & ~Gravity.AXIS_SPECIFIED) >> 1; |
| 1300 | |
| 1301 | maxAscent[index] = Math.max(maxAscent[index], childBaseline); |
| 1302 | maxDescent[index] = Math.max(maxDescent[index], |
| 1303 | childHeight - childBaseline); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | // Add in our padding |
| 1309 | mTotalLength += mPaddingLeft + mPaddingRight; |
Romain Guy | 053b480 | 2010-02-05 15:34:33 -0800 | [diff] [blame] | 1310 | // TODO: Should we update widthSize with the new total length? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | |
| 1312 | // Check mMaxAscent[INDEX_TOP] first because it maps to Gravity.TOP, |
| 1313 | // the most common case |
| 1314 | if (maxAscent[INDEX_TOP] != -1 || |
| 1315 | maxAscent[INDEX_CENTER_VERTICAL] != -1 || |
| 1316 | maxAscent[INDEX_BOTTOM] != -1 || |
| 1317 | maxAscent[INDEX_FILL] != -1) { |
| 1318 | final int ascent = Math.max(maxAscent[INDEX_FILL], |
| 1319 | Math.max(maxAscent[INDEX_CENTER_VERTICAL], |
| 1320 | Math.max(maxAscent[INDEX_TOP], maxAscent[INDEX_BOTTOM]))); |
| 1321 | final int descent = Math.max(maxDescent[INDEX_FILL], |
| 1322 | Math.max(maxDescent[INDEX_CENTER_VERTICAL], |
| 1323 | Math.max(maxDescent[INDEX_TOP], maxDescent[INDEX_BOTTOM]))); |
| 1324 | maxHeight = Math.max(maxHeight, ascent + descent); |
| 1325 | } |
| 1326 | } else { |
| 1327 | alternativeMaxHeight = Math.max(alternativeMaxHeight, weightedMaxHeight); |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1328 | |
| 1329 | // We have no limit, so make all weighted views as wide as the largest child. |
| 1330 | // Children will have already been measured once. |
Adam Powell | eabc919 | 2012-05-04 14:49:46 -0700 | [diff] [blame] | 1331 | if (useLargestChild && widthMode != MeasureSpec.EXACTLY) { |
Adam Powell | f8ac6b7 | 2011-05-23 18:14:09 -0700 | [diff] [blame] | 1332 | for (int i = 0; i < count; i++) { |
| 1333 | final View child = getVirtualChildAt(i); |
| 1334 | |
| 1335 | if (child == null || child.getVisibility() == View.GONE) { |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | final LinearLayout.LayoutParams lp = |
| 1340 | (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1341 | |
| 1342 | float childExtra = lp.weight; |
| 1343 | if (childExtra > 0) { |
| 1344 | child.measure( |
| 1345 | MeasureSpec.makeMeasureSpec(largestChildWidth, MeasureSpec.EXACTLY), |
| 1346 | MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(), |
| 1347 | MeasureSpec.EXACTLY)); |
| 1348 | } |
| 1349 | } |
| 1350 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | if (!allFillParent && heightMode != MeasureSpec.EXACTLY) { |
| 1354 | maxHeight = alternativeMaxHeight; |
| 1355 | } |
| 1356 | |
| 1357 | maxHeight += mPaddingTop + mPaddingBottom; |
| 1358 | |
| 1359 | // Check against our minimum height |
| 1360 | maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 1361 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1362 | setMeasuredDimension(widthSizeAndState | (childState&MEASURED_STATE_MASK), |
| 1363 | resolveSizeAndState(maxHeight, heightMeasureSpec, |
| 1364 | (childState<<MEASURED_HEIGHT_STATE_SHIFT))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | |
| 1366 | if (matchHeight) { |
| 1367 | forceUniformHeight(count, widthMeasureSpec); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | private void forceUniformHeight(int count, int widthMeasureSpec) { |
| 1372 | // Pretend that the linear layout has an exact size. This is the measured height of |
| 1373 | // ourselves. The measured height should be the max height of the children, changed |
Fabrice Di Meglio | fc53e58 | 2012-10-25 11:45:04 -0700 | [diff] [blame] | 1374 | // to accommodate the heightMeasureSpec from the parent |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | int uniformMeasureSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), |
| 1376 | MeasureSpec.EXACTLY); |
| 1377 | for (int i = 0; i < count; ++i) { |
| 1378 | final View child = getVirtualChildAt(i); |
| 1379 | if (child.getVisibility() != GONE) { |
| 1380 | LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1381 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1382 | if (lp.height == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | // Temporarily force children to reuse their old measured width |
| 1384 | // FIXME: this may not be right for something like wrapping text? |
| 1385 | int oldWidth = lp.width; |
| 1386 | lp.width = child.getMeasuredWidth(); |
| 1387 | |
| 1388 | // Remeasure with new dimensions |
| 1389 | measureChildWithMargins(child, widthMeasureSpec, 0, uniformMeasureSpec, 0); |
| 1390 | lp.width = oldWidth; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | /** |
| 1397 | * <p>Returns the number of children to skip after measuring/laying out |
| 1398 | * the specified child.</p> |
| 1399 | * |
| 1400 | * @param child the child after which we want to skip children |
| 1401 | * @param index the index of the child after which we want to skip children |
| 1402 | * @return the number of children to skip, 0 by default |
| 1403 | */ |
| 1404 | int getChildrenSkipCount(View child, int index) { |
| 1405 | return 0; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * <p>Returns the size (width or height) that should be occupied by a null |
| 1410 | * child.</p> |
| 1411 | * |
| 1412 | * @param childIndex the index of the null child |
| 1413 | * @return the width or height of the child depending on the orientation |
| 1414 | */ |
| 1415 | int measureNullChild(int childIndex) { |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | /** |
| 1420 | * <p>Measure the child according to the parent's measure specs. This |
| 1421 | * method should be overriden by subclasses to force the sizing of |
| 1422 | * children. This method is called by {@link #measureVertical(int, int)} and |
| 1423 | * {@link #measureHorizontal(int, int)}.</p> |
| 1424 | * |
| 1425 | * @param child the child to measure |
| 1426 | * @param childIndex the index of the child in this view |
| 1427 | * @param widthMeasureSpec horizontal space requirements as imposed by the parent |
| 1428 | * @param totalWidth extra space that has been used up by the parent horizontally |
| 1429 | * @param heightMeasureSpec vertical space requirements as imposed by the parent |
| 1430 | * @param totalHeight extra space that has been used up by the parent vertically |
| 1431 | */ |
| 1432 | void measureChildBeforeLayout(View child, int childIndex, |
| 1433 | int widthMeasureSpec, int totalWidth, int heightMeasureSpec, |
| 1434 | int totalHeight) { |
| 1435 | measureChildWithMargins(child, widthMeasureSpec, totalWidth, |
| 1436 | heightMeasureSpec, totalHeight); |
| 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * <p>Return the location offset of the specified child. This can be used |
| 1441 | * by subclasses to change the location of a given widget.</p> |
| 1442 | * |
| 1443 | * @param child the child for which to obtain the location offset |
| 1444 | * @return the location offset in pixels |
| 1445 | */ |
| 1446 | int getLocationOffset(View child) { |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | /** |
| 1451 | * <p>Return the size offset of the next sibling of the specified child. |
| 1452 | * This can be used by subclasses to change the location of the widget |
| 1453 | * following <code>child</code>.</p> |
| 1454 | * |
| 1455 | * @param child the child whose next sibling will be moved |
| 1456 | * @return the location offset of the next child in pixels |
| 1457 | */ |
| 1458 | int getNextLocationOffset(View child) { |
| 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | @Override |
| 1463 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 1464 | if (mOrientation == VERTICAL) { |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1465 | layoutVertical(l, t, r, b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1466 | } else { |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1467 | layoutHorizontal(l, t, r, b); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * Position the children during a layout pass if the orientation of this |
| 1473 | * LinearLayout is set to {@link #VERTICAL}. |
| 1474 | * |
| 1475 | * @see #getOrientation() |
| 1476 | * @see #setOrientation(int) |
| 1477 | * @see #onLayout(boolean, int, int, int, int) |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1478 | * @param left |
| 1479 | * @param top |
| 1480 | * @param right |
| 1481 | * @param bottom |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | */ |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1483 | void layoutVertical(int left, int top, int right, int bottom) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | final int paddingLeft = mPaddingLeft; |
| 1485 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1486 | int childTop; |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1487 | int childLeft; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | |
| 1489 | // Where right end of child should go |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1490 | final int width = right - left; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | int childRight = width - mPaddingRight; |
| 1492 | |
| 1493 | // Space available for child |
| 1494 | int childSpace = width - paddingLeft - mPaddingRight; |
| 1495 | |
| 1496 | final int count = getVirtualChildCount(); |
| 1497 | |
| 1498 | final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 1499 | final int minorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1501 | switch (majorGravity) { |
| 1502 | case Gravity.BOTTOM: |
| 1503 | // mTotalLength contains the padding already |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1504 | childTop = mPaddingTop + bottom - top - mTotalLength; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1505 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1506 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1507 | // mTotalLength contains the padding already |
| 1508 | case Gravity.CENTER_VERTICAL: |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1509 | childTop = mPaddingTop + (bottom - top - mTotalLength) / 2; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1510 | break; |
| 1511 | |
| 1512 | case Gravity.TOP: |
| 1513 | default: |
| 1514 | childTop = mPaddingTop; |
| 1515 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | } |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 1517 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1518 | for (int i = 0; i < count; i++) { |
| 1519 | final View child = getVirtualChildAt(i); |
| 1520 | if (child == null) { |
| 1521 | childTop += measureNullChild(i); |
| 1522 | } else if (child.getVisibility() != GONE) { |
| 1523 | final int childWidth = child.getMeasuredWidth(); |
| 1524 | final int childHeight = child.getMeasuredHeight(); |
| 1525 | |
| 1526 | final LinearLayout.LayoutParams lp = |
| 1527 | (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1528 | |
| 1529 | int gravity = lp.gravity; |
| 1530 | if (gravity < 0) { |
| 1531 | gravity = minorGravity; |
| 1532 | } |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 1533 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 1534 | final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection); |
Fabrice Di Meglio | de35cee | 2011-06-01 15:13:50 -0700 | [diff] [blame] | 1535 | switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1536 | case Gravity.CENTER_HORIZONTAL: |
| 1537 | childLeft = paddingLeft + ((childSpace - childWidth) / 2) |
| 1538 | + lp.leftMargin - lp.rightMargin; |
| 1539 | break; |
| 1540 | |
| 1541 | case Gravity.RIGHT: |
| 1542 | childLeft = childRight - childWidth - lp.rightMargin; |
| 1543 | break; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1544 | |
| 1545 | case Gravity.LEFT: |
Romain Guy | 611cd3f | 2009-12-15 12:00:37 -0800 | [diff] [blame] | 1546 | default: |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1547 | childLeft = paddingLeft + lp.leftMargin; |
Romain Guy | 611cd3f | 2009-12-15 12:00:37 -0800 | [diff] [blame] | 1548 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | } |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1550 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1551 | if (hasDividerBeforeChildAt(i)) { |
| 1552 | childTop += mDividerHeight; |
| 1553 | } |
| 1554 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | childTop += lp.topMargin; |
| 1556 | setChildFrame(child, childLeft, childTop + getLocationOffset(child), |
| 1557 | childWidth, childHeight); |
| 1558 | childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child); |
| 1559 | |
| 1560 | i += getChildrenSkipCount(child, i); |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | /** |
| 1566 | * Position the children during a layout pass if the orientation of this |
| 1567 | * LinearLayout is set to {@link #HORIZONTAL}. |
| 1568 | * |
| 1569 | * @see #getOrientation() |
| 1570 | * @see #setOrientation(int) |
| 1571 | * @see #onLayout(boolean, int, int, int, int) |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1572 | * @param left |
| 1573 | * @param top |
| 1574 | * @param right |
| 1575 | * @param bottom |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | */ |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1577 | void layoutHorizontal(int left, int top, int right, int bottom) { |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1578 | final boolean isLayoutRtl = isLayoutRtl(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | final int paddingTop = mPaddingTop; |
| 1580 | |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1581 | int childTop; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1582 | int childLeft; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | |
| 1584 | // Where bottom of child should go |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1585 | final int height = bottom - top; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | int childBottom = height - mPaddingBottom; |
| 1587 | |
| 1588 | // Space available for child |
| 1589 | int childSpace = height - paddingTop - mPaddingBottom; |
| 1590 | |
| 1591 | final int count = getVirtualChildCount(); |
| 1592 | |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 1593 | final int majorGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | final int minorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1595 | |
| 1596 | final boolean baselineAligned = mBaselineAligned; |
| 1597 | |
| 1598 | final int[] maxAscent = mMaxAscent; |
| 1599 | final int[] maxDescent = mMaxDescent; |
| 1600 | |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 1601 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 1602 | switch (Gravity.getAbsoluteGravity(majorGravity, layoutDirection)) { |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1603 | case Gravity.RIGHT: |
| 1604 | // mTotalLength contains the padding already |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1605 | childLeft = mPaddingLeft + right - left - mTotalLength; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1606 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1607 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1608 | case Gravity.CENTER_HORIZONTAL: |
| 1609 | // mTotalLength contains the padding already |
Philip Milne | ad365cc | 2012-09-27 14:38:46 -0700 | [diff] [blame] | 1610 | childLeft = mPaddingLeft + (right - left - mTotalLength) / 2; |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1611 | break; |
| 1612 | |
| 1613 | case Gravity.LEFT: |
| 1614 | default: |
| 1615 | childLeft = mPaddingLeft; |
| 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | int start = 0; |
| 1620 | int dir = 1; |
| 1621 | //In case of RTL, start drawing from the last child. |
| 1622 | if (isLayoutRtl) { |
| 1623 | start = count - 1; |
| 1624 | dir = -1; |
Adam Powell | fcca00a | 2010-11-30 21:26:29 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | for (int i = 0; i < count; i++) { |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1628 | int childIndex = start + dir * i; |
| 1629 | final View child = getVirtualChildAt(childIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | |
| 1631 | if (child == null) { |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1632 | childLeft += measureNullChild(childIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | } else if (child.getVisibility() != GONE) { |
| 1634 | final int childWidth = child.getMeasuredWidth(); |
| 1635 | final int childHeight = child.getMeasuredHeight(); |
| 1636 | int childBaseline = -1; |
| 1637 | |
| 1638 | final LinearLayout.LayoutParams lp = |
| 1639 | (LinearLayout.LayoutParams) child.getLayoutParams(); |
| 1640 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1641 | if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | childBaseline = child.getBaseline(); |
| 1643 | } |
| 1644 | |
| 1645 | int gravity = lp.gravity; |
| 1646 | if (gravity < 0) { |
| 1647 | gravity = minorGravity; |
| 1648 | } |
| 1649 | |
| 1650 | switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { |
| 1651 | case Gravity.TOP: |
| 1652 | childTop = paddingTop + lp.topMargin; |
| 1653 | if (childBaseline != -1) { |
| 1654 | childTop += maxAscent[INDEX_TOP] - childBaseline; |
| 1655 | } |
| 1656 | break; |
| 1657 | |
| 1658 | case Gravity.CENTER_VERTICAL: |
Romain Guy | 5b1b241 | 2010-01-21 19:09:51 -0800 | [diff] [blame] | 1659 | // Removed support for baseline alignment when layout_gravity or |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | // gravity == center_vertical. See bug #1038483. |
| 1661 | // Keep the code around if we need to re-enable this feature |
| 1662 | // if (childBaseline != -1) { |
| 1663 | // // Align baselines vertically only if the child is smaller than us |
| 1664 | // if (childSpace - childHeight > 0) { |
| 1665 | // childTop = paddingTop + (childSpace / 2) - childBaseline; |
| 1666 | // } else { |
| 1667 | // childTop = paddingTop + (childSpace - childHeight) / 2; |
| 1668 | // } |
| 1669 | // } else { |
| 1670 | childTop = paddingTop + ((childSpace - childHeight) / 2) |
| 1671 | + lp.topMargin - lp.bottomMargin; |
| 1672 | break; |
| 1673 | |
| 1674 | case Gravity.BOTTOM: |
| 1675 | childTop = childBottom - childHeight - lp.bottomMargin; |
| 1676 | if (childBaseline != -1) { |
| 1677 | int descent = child.getMeasuredHeight() - childBaseline; |
| 1678 | childTop -= (maxDescent[INDEX_BOTTOM] - descent); |
| 1679 | } |
| 1680 | break; |
Romain Guy | 611cd3f | 2009-12-15 12:00:37 -0800 | [diff] [blame] | 1681 | default: |
| 1682 | childTop = paddingTop; |
| 1683 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1686 | if (hasDividerBeforeChildAt(childIndex)) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1687 | childLeft += mDividerWidth; |
| 1688 | } |
| 1689 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | childLeft += lp.leftMargin; |
| 1691 | setChildFrame(child, childLeft + getLocationOffset(child), childTop, |
| 1692 | childWidth, childHeight); |
| 1693 | childLeft += childWidth + lp.rightMargin + |
| 1694 | getNextLocationOffset(child); |
| 1695 | |
Fabrice Di Meglio | 1e4cfbe | 2010-04-01 15:46:27 -0700 | [diff] [blame] | 1696 | i += getChildrenSkipCount(child, childIndex); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | private void setChildFrame(View child, int left, int top, int width, int height) { |
| 1702 | child.layout(left, top, left + width, top + height); |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * Should the layout be a column or a row. |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1707 | * @param orientation Pass {@link #HORIZONTAL} or {@link #VERTICAL}. Default |
| 1708 | * value is {@link #HORIZONTAL}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | * |
| 1710 | * @attr ref android.R.styleable#LinearLayout_orientation |
| 1711 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1712 | public void setOrientation(@OrientationMode int orientation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1713 | if (mOrientation != orientation) { |
| 1714 | mOrientation = orientation; |
| 1715 | requestLayout(); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * Returns the current orientation. |
| 1721 | * |
| 1722 | * @return either {@link #HORIZONTAL} or {@link #VERTICAL} |
| 1723 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 1724 | @OrientationMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1725 | public int getOrientation() { |
| 1726 | return mOrientation; |
| 1727 | } |
| 1728 | |
| 1729 | /** |
| 1730 | * Describes how the child views are positioned. Defaults to GRAVITY_TOP. If |
| 1731 | * this layout has a VERTICAL orientation, this controls where all the child |
| 1732 | * views are placed if there is extra vertical space. If this layout has a |
| 1733 | * HORIZONTAL orientation, this controls the alignment of the children. |
| 1734 | * |
| 1735 | * @param gravity See {@link android.view.Gravity} |
| 1736 | * |
| 1737 | * @attr ref android.R.styleable#LinearLayout_gravity |
| 1738 | */ |
| 1739 | @android.view.RemotableViewMethod |
| 1740 | public void setGravity(int gravity) { |
| 1741 | if (mGravity != gravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 1742 | if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) { |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 1743 | gravity |= Gravity.START; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) { |
| 1747 | gravity |= Gravity.TOP; |
| 1748 | } |
| 1749 | |
| 1750 | mGravity = gravity; |
| 1751 | requestLayout(); |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | @android.view.RemotableViewMethod |
| 1756 | public void setHorizontalGravity(int horizontalGravity) { |
Fabrice Di Meglio | 6a03640 | 2011-05-23 14:43:23 -0700 | [diff] [blame] | 1757 | final int gravity = horizontalGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 1758 | if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) { |
| 1759 | mGravity = (mGravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | requestLayout(); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | @android.view.RemotableViewMethod |
| 1765 | public void setVerticalGravity(int verticalGravity) { |
| 1766 | final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK; |
| 1767 | if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) { |
| 1768 | mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity; |
| 1769 | requestLayout(); |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | @Override |
| 1774 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 1775 | return new LinearLayout.LayoutParams(getContext(), attrs); |
| 1776 | } |
| 1777 | |
| 1778 | /** |
| 1779 | * Returns a set of layout parameters with a width of |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1780 | * {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | * and a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} |
| 1782 | * when the layout's orientation is {@link #VERTICAL}. When the orientation is |
| 1783 | * {@link #HORIZONTAL}, the width is set to {@link LayoutParams#WRAP_CONTENT} |
| 1784 | * and the height to {@link LayoutParams#WRAP_CONTENT}. |
| 1785 | */ |
| 1786 | @Override |
| 1787 | protected LayoutParams generateDefaultLayoutParams() { |
| 1788 | if (mOrientation == HORIZONTAL) { |
| 1789 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 1790 | } else if (mOrientation == VERTICAL) { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1791 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | } |
| 1793 | return null; |
| 1794 | } |
| 1795 | |
| 1796 | @Override |
| 1797 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 1798 | return new LayoutParams(p); |
| 1799 | } |
| 1800 | |
| 1801 | |
| 1802 | // Override to allow type-checking of LayoutParams. |
| 1803 | @Override |
| 1804 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 1805 | return p instanceof LinearLayout.LayoutParams; |
| 1806 | } |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1807 | |
| 1808 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1809 | public CharSequence getAccessibilityClassName() { |
| 1810 | return LinearLayout.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | /** |
| 1814 | * Per-child layout information associated with ViewLinearLayout. |
| 1815 | * |
| 1816 | * @attr ref android.R.styleable#LinearLayout_Layout_layout_weight |
| 1817 | * @attr ref android.R.styleable#LinearLayout_Layout_layout_gravity |
| 1818 | */ |
| 1819 | public static class LayoutParams extends ViewGroup.MarginLayoutParams { |
| 1820 | /** |
| 1821 | * Indicates how much of the extra space in the LinearLayout will be |
| 1822 | * allocated to the view associated with these LayoutParams. Specify |
| 1823 | * 0 if the view should not be stretched. Otherwise the extra pixels |
| 1824 | * will be pro-rated among all views whose weight is greater than 0. |
| 1825 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1826 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | public float weight; |
| 1828 | |
| 1829 | /** |
| 1830 | * Gravity for the view associated with these LayoutParams. |
| 1831 | * |
| 1832 | * @see android.view.Gravity |
| 1833 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 1834 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1835 | @ViewDebug.IntToString(from = -1, to = "NONE"), |
| 1836 | @ViewDebug.IntToString(from = Gravity.NO_GRAVITY, to = "NONE"), |
| 1837 | @ViewDebug.IntToString(from = Gravity.TOP, to = "TOP"), |
| 1838 | @ViewDebug.IntToString(from = Gravity.BOTTOM, to = "BOTTOM"), |
| 1839 | @ViewDebug.IntToString(from = Gravity.LEFT, to = "LEFT"), |
| 1840 | @ViewDebug.IntToString(from = Gravity.RIGHT, to = "RIGHT"), |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 1841 | @ViewDebug.IntToString(from = Gravity.START, to = "START"), |
| 1842 | @ViewDebug.IntToString(from = Gravity.END, to = "END"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1843 | @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL, to = "CENTER_VERTICAL"), |
| 1844 | @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL, to = "FILL_VERTICAL"), |
| 1845 | @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"), |
| 1846 | @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL, to = "FILL_HORIZONTAL"), |
| 1847 | @ViewDebug.IntToString(from = Gravity.CENTER, to = "CENTER"), |
| 1848 | @ViewDebug.IntToString(from = Gravity.FILL, to = "FILL") |
| 1849 | }) |
| 1850 | public int gravity = -1; |
| 1851 | |
| 1852 | /** |
| 1853 | * {@inheritDoc} |
| 1854 | */ |
| 1855 | public LayoutParams(Context c, AttributeSet attrs) { |
| 1856 | super(c, attrs); |
| 1857 | TypedArray a = |
| 1858 | c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout); |
| 1859 | |
| 1860 | weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0); |
| 1861 | gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1); |
| 1862 | |
| 1863 | a.recycle(); |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * {@inheritDoc} |
| 1868 | */ |
| 1869 | public LayoutParams(int width, int height) { |
| 1870 | super(width, height); |
| 1871 | weight = 0; |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * Creates a new set of layout parameters with the specified width, height |
| 1876 | * and weight. |
| 1877 | * |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1878 | * @param width the width, either {@link #MATCH_PARENT}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | * {@link #WRAP_CONTENT} or a fixed size in pixels |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1880 | * @param height the height, either {@link #MATCH_PARENT}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1881 | * {@link #WRAP_CONTENT} or a fixed size in pixels |
| 1882 | * @param weight the weight |
| 1883 | */ |
| 1884 | public LayoutParams(int width, int height, float weight) { |
| 1885 | super(width, height); |
| 1886 | this.weight = weight; |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * {@inheritDoc} |
| 1891 | */ |
| 1892 | public LayoutParams(ViewGroup.LayoutParams p) { |
| 1893 | super(p); |
| 1894 | } |
| 1895 | |
| 1896 | /** |
| 1897 | * {@inheritDoc} |
| 1898 | */ |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 1899 | public LayoutParams(ViewGroup.MarginLayoutParams source) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1900 | super(source); |
| 1901 | } |
| 1902 | |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 1903 | /** |
| 1904 | * Copy constructor. Clones the width, height, margin values, weight, |
| 1905 | * and gravity of the source. |
| 1906 | * |
| 1907 | * @param source The layout params to copy from. |
| 1908 | */ |
| 1909 | public LayoutParams(LayoutParams source) { |
| 1910 | super(source); |
| 1911 | |
| 1912 | this.weight = source.weight; |
| 1913 | this.gravity = source.gravity; |
| 1914 | } |
| 1915 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | @Override |
| 1917 | public String debug(String output) { |
| 1918 | return output + "LinearLayout.LayoutParams={width=" + sizeToString(width) + |
| 1919 | ", height=" + sizeToString(height) + " weight=" + weight + "}"; |
| 1920 | } |
| 1921 | } |
| 1922 | } |