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 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 19 | import com.android.internal.R; |
Fabrice Di Meglio | de35cee | 2011-06-01 15:13:50 -0700 | [diff] [blame] | 20 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 21 | import android.annotation.AttrRes; |
Alan Viverette | 96ccd39 | 2015-01-30 15:55:48 -0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Alan Viverette | 9117436 | 2014-06-17 14:51:45 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 24 | import android.annotation.StyleRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.res.TypedArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.util.AttributeSet; |
Fabrice Di Meglio | de35cee | 2011-06-01 15:13:50 -0700 | [diff] [blame] | 30 | import android.view.Gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.view.View; |
Dianne Hackborn | 958b9ad | 2009-03-31 18:00:36 -0700 | [diff] [blame] | 32 | import android.view.ViewDebug; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.view.ViewGroup; |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 34 | import android.view.ViewHierarchyEncoder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.widget.RemoteViews.RemoteView; |
| 36 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 37 | import java.util.ArrayList; |
Chet Haase | 4610eef | 2015-12-03 07:38:11 -0800 | [diff] [blame] | 38 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | /** |
| 40 | * FrameLayout is designed to block out an area on the screen to display |
Scott Main | 812634c2 | 2011-07-27 13:22:35 -0700 | [diff] [blame] | 41 | * a single item. Generally, FrameLayout should be used to hold a single child view, because it can |
| 42 | * be difficult to organize child views in a way that's scalable to different screen sizes without |
| 43 | * the children overlapping each other. You can, however, add multiple children to a FrameLayout |
| 44 | * and control their position within the FrameLayout by assigning gravity to each child, using the |
| 45 | * <a href="FrameLayout.LayoutParams.html#attr_android:layout_gravity">{@code |
| 46 | * android:layout_gravity}</a> attribute. |
| 47 | * <p>Child views are drawn in a stack, with the most recently added child on top. |
| 48 | * The size of the FrameLayout is the size of its largest child (plus padding), visible |
| 49 | * or not (if the FrameLayout's parent permits). Views that are {@link android.view.View#GONE} are |
| 50 | * used for sizing |
| 51 | * only if {@link #setMeasureAllChildren(boolean) setConsiderGoneChildrenWhenMeasuring()} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | * is set to true. |
| 53 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | * @attr ref android.R.styleable#FrameLayout_measureAllChildren |
| 55 | */ |
| 56 | @RemoteView |
| 57 | public class FrameLayout extends ViewGroup { |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 58 | private static final int DEFAULT_CHILD_GRAVITY = Gravity.TOP | Gravity.START; |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 59 | |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 60 | @ViewDebug.ExportedProperty(category = "measurement") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | boolean mMeasureAllChildren = false; |
| 62 | |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 63 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | private int mForegroundPaddingLeft = 0; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 65 | |
| 66 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | private int mForegroundPaddingTop = 0; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 68 | |
| 69 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | private int mForegroundPaddingRight = 0; |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 71 | |
| 72 | @ViewDebug.ExportedProperty(category = "padding") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | private int mForegroundPaddingBottom = 0; |
| 74 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 75 | private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1); |
| 76 | |
| 77 | public FrameLayout(@NonNull Context context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | super(context); |
| 79 | } |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 80 | |
| 81 | public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | this(context, attrs, 0); |
| 83 | } |
| 84 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 85 | public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, |
| 86 | @AttrRes int defStyleAttr) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 87 | this(context, attrs, defStyleAttr, 0); |
| 88 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 90 | public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, |
| 91 | @AttrRes int defStyleAttr, @StyleRes int defStyleRes) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 92 | super(context, attrs, defStyleAttr, defStyleRes); |
| 93 | |
| 94 | final TypedArray a = context.obtainStyledAttributes( |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 95 | attrs, R.styleable.FrameLayout, defStyleAttr, defStyleRes); |
| 96 | |
| 97 | if (a.getBoolean(R.styleable.FrameLayout_measureAllChildren, false)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | setMeasureAllChildren(true); |
| 99 | } |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | a.recycle(); |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Fabrice Di Meglio | 9e3b002 | 2011-06-06 16:30:29 -0700 | [diff] [blame] | 105 | * Describes how the foreground is positioned. Defaults to START and TOP. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | * |
| 107 | * @param foregroundGravity See {@link android.view.Gravity} |
| 108 | * |
Philip Milne | 1018fb4 | 2012-03-13 12:00:04 -0700 | [diff] [blame] | 109 | * @see #getForegroundGravity() |
| 110 | * |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 111 | * @attr ref android.R.styleable#View_foregroundGravity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | */ |
| 113 | @android.view.RemotableViewMethod |
| 114 | public void setForegroundGravity(int foregroundGravity) { |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 115 | if (getForegroundGravity() != foregroundGravity) { |
| 116 | super.setForegroundGravity(foregroundGravity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 118 | // calling get* again here because the set above may apply default constraints |
| 119 | final Drawable foreground = getForeground(); |
| 120 | if (getForegroundGravity() == Gravity.FILL && foreground != null) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 121 | Rect padding = new Rect(); |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 122 | if (foreground.getPadding(padding)) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 123 | mForegroundPaddingLeft = padding.left; |
| 124 | mForegroundPaddingTop = padding.top; |
| 125 | mForegroundPaddingRight = padding.right; |
| 126 | mForegroundPaddingBottom = padding.bottom; |
| 127 | } |
| 128 | } else { |
| 129 | mForegroundPaddingLeft = 0; |
| 130 | mForegroundPaddingTop = 0; |
| 131 | mForegroundPaddingRight = 0; |
| 132 | mForegroundPaddingBottom = 0; |
| 133 | } |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | requestLayout(); |
| 136 | } |
| 137 | } |
| 138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | /** |
| 140 | * Returns a set of layout parameters with a width of |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 141 | * {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT}, |
| 142 | * and a height of {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | */ |
| 144 | @Override |
| 145 | protected LayoutParams generateDefaultLayoutParams() { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 146 | return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Fabrice Di Meglio | c4d7122 | 2013-06-11 19:13:54 -0700 | [diff] [blame] | 149 | int getPaddingLeftWithForeground() { |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 150 | return isForegroundInsidePadding() ? Math.max(mPaddingLeft, mForegroundPaddingLeft) : |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 151 | mPaddingLeft + mForegroundPaddingLeft; |
| 152 | } |
| 153 | |
Fabrice Di Meglio | c4d7122 | 2013-06-11 19:13:54 -0700 | [diff] [blame] | 154 | int getPaddingRightWithForeground() { |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 155 | return isForegroundInsidePadding() ? Math.max(mPaddingRight, mForegroundPaddingRight) : |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 156 | mPaddingRight + mForegroundPaddingRight; |
| 157 | } |
| 158 | |
| 159 | private int getPaddingTopWithForeground() { |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 160 | return isForegroundInsidePadding() ? Math.max(mPaddingTop, mForegroundPaddingTop) : |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 161 | mPaddingTop + mForegroundPaddingTop; |
| 162 | } |
| 163 | |
| 164 | private int getPaddingBottomWithForeground() { |
Adam Powell | 2b25e2e | 2015-03-23 16:33:32 -0700 | [diff] [blame] | 165 | return isForegroundInsidePadding() ? Math.max(mPaddingBottom, mForegroundPaddingBottom) : |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 166 | mPaddingBottom + mForegroundPaddingBottom; |
| 167 | } |
| 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | @Override |
| 170 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 171 | int count = getChildCount(); |
| 172 | |
| 173 | final boolean measureMatchParentChildren = |
| 174 | MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY || |
| 175 | MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY; |
| 176 | mMatchParentChildren.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | int maxHeight = 0; |
| 179 | int maxWidth = 0; |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 180 | int childState = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | for (int i = 0; i < count; i++) { |
| 183 | final View child = getChildAt(i); |
| 184 | if (mMeasureAllChildren || child.getVisibility() != GONE) { |
| 185 | measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); |
Adam Powell | 2b6be70 | 2011-01-08 16:44:07 -0800 | [diff] [blame] | 186 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 187 | maxWidth = Math.max(maxWidth, |
| 188 | child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); |
| 189 | maxHeight = Math.max(maxHeight, |
| 190 | child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 191 | childState = combineMeasuredStates(childState, child.getMeasuredState()); |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 192 | if (measureMatchParentChildren) { |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 193 | if (lp.width == LayoutParams.MATCH_PARENT || |
| 194 | lp.height == LayoutParams.MATCH_PARENT) { |
| 195 | mMatchParentChildren.add(child); |
| 196 | } |
| 197 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | // Account for padding too |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 202 | maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground(); |
| 203 | maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
| 205 | // Check against our minimum height and width |
| 206 | maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 207 | maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 208 | |
| 209 | // Check against our foreground's minimum height and width |
| 210 | final Drawable drawable = getForeground(); |
| 211 | if (drawable != null) { |
| 212 | maxHeight = Math.max(maxHeight, drawable.getMinimumHeight()); |
| 213 | maxWidth = Math.max(maxWidth, drawable.getMinimumWidth()); |
| 214 | } |
| 215 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 216 | setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), |
| 217 | resolveSizeAndState(maxHeight, heightMeasureSpec, |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 218 | childState << MEASURED_HEIGHT_STATE_SHIFT)); |
| 219 | |
Romain Guy | a174d7a | 2011-01-07 13:27:39 -0800 | [diff] [blame] | 220 | count = mMatchParentChildren.size(); |
| 221 | if (count > 1) { |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 222 | for (int i = 0; i < count; i++) { |
| 223 | final View child = mMatchParentChildren.get(i); |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 224 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
Alan Viverette | 39fd902 | 2015-06-25 12:24:27 -0700 | [diff] [blame] | 225 | |
| 226 | final int childWidthMeasureSpec; |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 227 | if (lp.width == LayoutParams.MATCH_PARENT) { |
Alan Viverette | 39fd902 | 2015-06-25 12:24:27 -0700 | [diff] [blame] | 228 | final int width = Math.max(0, getMeasuredWidth() |
| 229 | - getPaddingLeftWithForeground() - getPaddingRightWithForeground() |
| 230 | - lp.leftMargin - lp.rightMargin); |
| 231 | childWidthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 232 | width, MeasureSpec.EXACTLY); |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 233 | } else { |
| 234 | childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 235 | getPaddingLeftWithForeground() + getPaddingRightWithForeground() + |
| 236 | lp.leftMargin + lp.rightMargin, |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 237 | lp.width); |
| 238 | } |
Alan Viverette | 39fd902 | 2015-06-25 12:24:27 -0700 | [diff] [blame] | 239 | |
| 240 | final int childHeightMeasureSpec; |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 241 | if (lp.height == LayoutParams.MATCH_PARENT) { |
Alan Viverette | 39fd902 | 2015-06-25 12:24:27 -0700 | [diff] [blame] | 242 | final int height = Math.max(0, getMeasuredHeight() |
| 243 | - getPaddingTopWithForeground() - getPaddingBottomWithForeground() |
| 244 | - lp.topMargin - lp.bottomMargin); |
| 245 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 246 | height, MeasureSpec.EXACTLY); |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 247 | } else { |
Romain Guy | cf70dcb | 2011-01-07 11:03:20 -0800 | [diff] [blame] | 248 | childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 249 | getPaddingTopWithForeground() + getPaddingBottomWithForeground() + |
| 250 | lp.topMargin + lp.bottomMargin, |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 251 | lp.height); |
| 252 | } |
| 253 | |
| 254 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 255 | } |
| 256 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | } |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | @Override |
| 260 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Fabrice Di Meglio | c4d7122 | 2013-06-11 19:13:54 -0700 | [diff] [blame] | 261 | layoutChildren(left, top, right, bottom, false /* no force left gravity */); |
| 262 | } |
| 263 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 264 | void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | final int count = getChildCount(); |
| 266 | |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 267 | final int parentLeft = getPaddingLeftWithForeground(); |
| 268 | final int parentRight = right - left - getPaddingRightWithForeground(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | |
Michael Jurka | 02473da | 2011-09-08 18:26:23 -0700 | [diff] [blame] | 270 | final int parentTop = getPaddingTopWithForeground(); |
| 271 | final int parentBottom = bottom - top - getPaddingBottomWithForeground(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
| 273 | for (int i = 0; i < count; i++) { |
| 274 | final View child = getChildAt(i); |
| 275 | if (child.getVisibility() != GONE) { |
| 276 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 277 | |
| 278 | final int width = child.getMeasuredWidth(); |
| 279 | final int height = child.getMeasuredHeight(); |
| 280 | |
Romain Guy | 9c95737 | 2011-01-04 17:39:43 -0800 | [diff] [blame] | 281 | int childLeft; |
| 282 | int childTop; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | |
Adam Powell | 5e0ae67 | 2010-12-06 21:33:04 -0800 | [diff] [blame] | 284 | int gravity = lp.gravity; |
| 285 | if (gravity == -1) { |
| 286 | gravity = DEFAULT_CHILD_GRAVITY; |
| 287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 289 | final int layoutDirection = getLayoutDirection(); |
Fabrice Di Meglio | c005322 | 2011-06-13 12:16:51 -0700 | [diff] [blame] | 290 | final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection); |
Adam Powell | 5e0ae67 | 2010-12-06 21:33:04 -0800 | [diff] [blame] | 291 | final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Fabrice Di Meglio | de35cee | 2011-06-01 15:13:50 -0700 | [diff] [blame] | 293 | switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) { |
Adam Powell | 5e0ae67 | 2010-12-06 21:33:04 -0800 | [diff] [blame] | 294 | case Gravity.CENTER_HORIZONTAL: |
| 295 | childLeft = parentLeft + (parentRight - parentLeft - width) / 2 + |
| 296 | lp.leftMargin - lp.rightMargin; |
| 297 | break; |
| 298 | case Gravity.RIGHT: |
Fabrice Di Meglio | c4d7122 | 2013-06-11 19:13:54 -0700 | [diff] [blame] | 299 | if (!forceLeftGravity) { |
| 300 | childLeft = parentRight - width - lp.rightMargin; |
| 301 | break; |
| 302 | } |
| 303 | case Gravity.LEFT: |
Adam Powell | 5e0ae67 | 2010-12-06 21:33:04 -0800 | [diff] [blame] | 304 | default: |
| 305 | childLeft = parentLeft + lp.leftMargin; |
| 306 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | |
Adam Powell | 5e0ae67 | 2010-12-06 21:33:04 -0800 | [diff] [blame] | 308 | switch (verticalGravity) { |
| 309 | case Gravity.TOP: |
| 310 | childTop = parentTop + lp.topMargin; |
| 311 | break; |
| 312 | case Gravity.CENTER_VERTICAL: |
| 313 | childTop = parentTop + (parentBottom - parentTop - height) / 2 + |
| 314 | lp.topMargin - lp.bottomMargin; |
| 315 | break; |
| 316 | case Gravity.BOTTOM: |
| 317 | childTop = parentBottom - height - lp.bottomMargin; |
| 318 | break; |
| 319 | default: |
| 320 | childTop = parentTop + lp.topMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | child.layout(childLeft, childTop, childLeft + width, childTop + height); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 329 | * Sets whether to consider all children, or just those in |
| 330 | * the VISIBLE or INVISIBLE state, when measuring. Defaults to false. |
| 331 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | * @param measureAll true to consider children marked GONE, false otherwise. |
| 333 | * Default value is false. |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 334 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | * @attr ref android.R.styleable#FrameLayout_measureAllChildren |
| 336 | */ |
| 337 | @android.view.RemotableViewMethod |
| 338 | public void setMeasureAllChildren(boolean measureAll) { |
| 339 | mMeasureAllChildren = measureAll; |
| 340 | } |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 342 | /** |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 343 | * Determines whether all children, or just those in the VISIBLE or |
| 344 | * INVISIBLE state, are considered when measuring. |
| 345 | * |
| 346 | * @return Whether all children are considered when measuring. |
| 347 | * |
| 348 | * @deprecated This method is deprecated in favor of |
| 349 | * {@link #getMeasureAllChildren() getMeasureAllChildren()}, which was |
| 350 | * renamed for consistency with |
| 351 | * {@link #setMeasureAllChildren(boolean) setMeasureAllChildren()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | */ |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 353 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | public boolean getConsiderGoneChildrenWhenMeasuring() { |
Steve Block | b25825a | 2011-04-27 14:51:38 +0100 | [diff] [blame] | 355 | return getMeasureAllChildren(); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Determines whether all children, or just those in the VISIBLE or |
| 360 | * INVISIBLE state, are considered when measuring. |
| 361 | * |
| 362 | * @return Whether all children are considered when measuring. |
| 363 | */ |
| 364 | public boolean getMeasureAllChildren() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | return mMeasureAllChildren; |
| 366 | } |
| 367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | @Override |
| 369 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 370 | return new FrameLayout.LayoutParams(getContext(), attrs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | } |
| 372 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 373 | @Override |
| 374 | public boolean shouldDelayChildPressedState() { |
| 375 | return false; |
| 376 | } |
| 377 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | @Override |
| 379 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 380 | return p instanceof LayoutParams; |
| 381 | } |
| 382 | |
| 383 | @Override |
Yigit Boyar | 885c50b | 2016-03-22 16:53:42 -0700 | [diff] [blame] | 384 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) { |
| 385 | if (lp instanceof LayoutParams) { |
| 386 | return new LayoutParams((LayoutParams) lp); |
| 387 | } else if (lp instanceof MarginLayoutParams) { |
| 388 | return new LayoutParams((MarginLayoutParams) lp); |
| 389 | } else { |
| 390 | return new LayoutParams(lp); |
| 391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 394 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 395 | public CharSequence getAccessibilityClassName() { |
| 396 | return FrameLayout.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 397 | } |
| 398 | |
Siva Velusamy | 94a6d15 | 2015-05-05 15:07:00 -0700 | [diff] [blame] | 399 | /** @hide */ |
| 400 | @Override |
| 401 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 402 | super.encodeProperties(encoder); |
| 403 | |
| 404 | encoder.addProperty("measurement:measureAllChildren", mMeasureAllChildren); |
| 405 | encoder.addProperty("padding:foregroundPaddingLeft", mForegroundPaddingLeft); |
| 406 | encoder.addProperty("padding:foregroundPaddingTop", mForegroundPaddingTop); |
| 407 | encoder.addProperty("padding:foregroundPaddingRight", mForegroundPaddingRight); |
| 408 | encoder.addProperty("padding:foregroundPaddingBottom", mForegroundPaddingBottom); |
| 409 | } |
| 410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | /** |
| 412 | * Per-child layout information for layouts that support margins. |
| 413 | * See {@link android.R.styleable#FrameLayout_Layout FrameLayout Layout Attributes} |
| 414 | * for a list of all child view attributes that this class supports. |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 415 | * |
Romain Guy | 606e8cc | 2010-08-17 12:43:05 -0700 | [diff] [blame] | 416 | * @attr ref android.R.styleable#FrameLayout_Layout_layout_gravity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | */ |
| 418 | public static class LayoutParams extends MarginLayoutParams { |
| 419 | /** |
| 420 | * The gravity to apply with the View to which these layout parameters |
| 421 | * are associated. |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 422 | * <p> |
| 423 | * The default value is {@code Gravity.TOP | Gravity.START} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | * |
| 425 | * @see android.view.Gravity |
Romain Guy | 606e8cc | 2010-08-17 12:43:05 -0700 | [diff] [blame] | 426 | * @attr ref android.R.styleable#FrameLayout_Layout_layout_gravity |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | */ |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 428 | public int gravity = DEFAULT_CHILD_GRAVITY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 429 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 430 | public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | super(c, attrs); |
| 432 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 433 | final TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.FrameLayout_Layout); |
| 434 | gravity = a.getInt(R.styleable.FrameLayout_Layout_layout_gravity, |
| 435 | DEFAULT_CHILD_GRAVITY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | a.recycle(); |
| 437 | } |
| 438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 439 | public LayoutParams(int width, int height) { |
| 440 | super(width, height); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Creates a new set of layout parameters with the specified width, height |
| 445 | * and weight. |
| 446 | * |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 447 | * @param width the width, either {@link #MATCH_PARENT}, |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 448 | * {@link #WRAP_CONTENT} or a fixed size in pixels |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 449 | * @param height the height, either {@link #MATCH_PARENT}, |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 450 | * {@link #WRAP_CONTENT} or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | * @param gravity the gravity |
| 452 | * |
| 453 | * @see android.view.Gravity |
| 454 | */ |
| 455 | public LayoutParams(int width, int height, int gravity) { |
| 456 | super(width, height); |
| 457 | this.gravity = gravity; |
| 458 | } |
| 459 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 460 | public LayoutParams(@NonNull ViewGroup.LayoutParams source) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | super(source); |
| 462 | } |
| 463 | |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 464 | public LayoutParams(@NonNull ViewGroup.MarginLayoutParams source) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | super(source); |
| 466 | } |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * Copy constructor. Clones the width, height, margin values, and |
| 470 | * gravity of the source. |
| 471 | * |
| 472 | * @param source The layout params to copy from. |
| 473 | */ |
Alan Viverette | bd53c98 | 2016-03-31 16:55:06 -0400 | [diff] [blame] | 474 | public LayoutParams(@NonNull LayoutParams source) { |
Alan Viverette | 0a0e155 | 2013-08-07 13:24:09 -0700 | [diff] [blame] | 475 | super(source); |
| 476 | |
| 477 | this.gravity = source.gravity; |
| 478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | } |