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