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