blob: 9ac49172ec89abdde58973de756cdddc037532db [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.widget;
18
Alan Viverettebd53c982016-03-31 16:55:06 -040019import com.android.internal.R;
Fabrice Di Megliode35cee2011-06-01 15:13:50 -070020
Alan Viverettebd53c982016-03-31 16:55:06 -040021import android.annotation.AttrRes;
Alan Viverette96ccd392015-01-30 15:55:48 -080022import android.annotation.NonNull;
Alan Viverette91174362014-06-17 14:51:45 -070023import android.annotation.Nullable;
Alan Viverettebd53c982016-03-31 16:55:06 -040024import android.annotation.StyleRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.res.TypedArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.graphics.Rect;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.drawable.Drawable;
29import android.util.AttributeSet;
Fabrice Di Megliode35cee2011-06-01 15:13:50 -070030import android.view.Gravity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.view.View;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070032import android.view.ViewDebug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.view.ViewGroup;
Siva Velusamy94a6d152015-05-05 15:07:00 -070034import android.view.ViewHierarchyEncoder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.widget.RemoteViews.RemoteView;
36
Alan Viverettebd53c982016-03-31 16:55:06 -040037import java.util.ArrayList;
Chet Haase4610eef2015-12-03 07:38:11 -080038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039/**
40 * FrameLayout is designed to block out an area on the screen to display
Scott Main812634c22011-07-27 13:22:35 -070041 * 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 Project9066cfe2009-03-03 19:31:44 -080052 * is set to true.
53 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 * @attr ref android.R.styleable#FrameLayout_measureAllChildren
55 */
56@RemoteView
57public class FrameLayout extends ViewGroup {
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -070058 private static final int DEFAULT_CHILD_GRAVITY = Gravity.TOP | Gravity.START;
Romain Guy9c957372011-01-04 17:39:43 -080059
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070060 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 boolean mMeasureAllChildren = false;
62
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070063 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 private int mForegroundPaddingLeft = 0;
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070065
66 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 private int mForegroundPaddingTop = 0;
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070068
69 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 private int mForegroundPaddingRight = 0;
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070071
72 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private int mForegroundPaddingBottom = 0;
74
Alan Viverettebd53c982016-03-31 16:55:06 -040075 private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1);
76
77 public FrameLayout(@NonNull Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 super(context);
79 }
Alan Viverettebd53c982016-03-31 16:55:06 -040080
81 public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 this(context, attrs, 0);
83 }
84
Alan Viverettebd53c982016-03-31 16:55:06 -040085 public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs,
86 @AttrRes int defStyleAttr) {
Alan Viverette617feb92013-09-09 18:09:13 -070087 this(context, attrs, defStyleAttr, 0);
88 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Alan Viverettebd53c982016-03-31 16:55:06 -040090 public FrameLayout(@NonNull Context context, @Nullable AttributeSet attrs,
91 @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
Alan Viverette617feb92013-09-09 18:09:13 -070092 super(context, attrs, defStyleAttr, defStyleRes);
93
94 final TypedArray a = context.obtainStyledAttributes(
Alan Viverettebd53c982016-03-31 16:55:06 -040095 attrs, R.styleable.FrameLayout, defStyleAttr, defStyleRes);
96
97 if (a.getBoolean(R.styleable.FrameLayout_measureAllChildren, false)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 setMeasureAllChildren(true);
99 }
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 a.recycle();
Philip Milne1018fb42012-03-13 12:00:04 -0700102 }
103
104 /**
Fabrice Di Meglio9e3b0022011-06-06 16:30:29 -0700105 * Describes how the foreground is positioned. Defaults to START and TOP.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 *
107 * @param foregroundGravity See {@link android.view.Gravity}
108 *
Philip Milne1018fb42012-03-13 12:00:04 -0700109 * @see #getForegroundGravity()
110 *
Adam Powell2b25e2e2015-03-23 16:33:32 -0700111 * @attr ref android.R.styleable#View_foregroundGravity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 */
113 @android.view.RemotableViewMethod
114 public void setForegroundGravity(int foregroundGravity) {
Adam Powell2b25e2e2015-03-23 16:33:32 -0700115 if (getForegroundGravity() != foregroundGravity) {
116 super.setForegroundGravity(foregroundGravity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Adam Powell2b25e2e2015-03-23 16:33:32 -0700118 // 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 Project10592532009-03-18 17:39:46 -0700121 Rect padding = new Rect();
Adam Powell2b25e2e2015-03-23 16:33:32 -0700122 if (foreground.getPadding(padding)) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700123 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 Project9066cfe2009-03-03 19:31:44 -0800135 requestLayout();
136 }
137 }
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 /**
140 * Returns a set of layout parameters with a width of
Romain Guy980a9382010-01-08 15:06:28 -0800141 * {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT},
142 * and a height of {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 */
144 @Override
145 protected LayoutParams generateDefaultLayoutParams() {
Romain Guy980a9382010-01-08 15:06:28 -0800146 return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
148
Fabrice Di Meglioc4d71222013-06-11 19:13:54 -0700149 int getPaddingLeftWithForeground() {
Adam Powell2b25e2e2015-03-23 16:33:32 -0700150 return isForegroundInsidePadding() ? Math.max(mPaddingLeft, mForegroundPaddingLeft) :
Michael Jurka02473da2011-09-08 18:26:23 -0700151 mPaddingLeft + mForegroundPaddingLeft;
152 }
153
Fabrice Di Meglioc4d71222013-06-11 19:13:54 -0700154 int getPaddingRightWithForeground() {
Adam Powell2b25e2e2015-03-23 16:33:32 -0700155 return isForegroundInsidePadding() ? Math.max(mPaddingRight, mForegroundPaddingRight) :
Michael Jurka02473da2011-09-08 18:26:23 -0700156 mPaddingRight + mForegroundPaddingRight;
157 }
158
159 private int getPaddingTopWithForeground() {
Adam Powell2b25e2e2015-03-23 16:33:32 -0700160 return isForegroundInsidePadding() ? Math.max(mPaddingTop, mForegroundPaddingTop) :
Michael Jurka02473da2011-09-08 18:26:23 -0700161 mPaddingTop + mForegroundPaddingTop;
162 }
163
164 private int getPaddingBottomWithForeground() {
Adam Powell2b25e2e2015-03-23 16:33:32 -0700165 return isForegroundInsidePadding() ? Math.max(mPaddingBottom, mForegroundPaddingBottom) :
Michael Jurka02473da2011-09-08 18:26:23 -0700166 mPaddingBottom + mForegroundPaddingBottom;
167 }
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 @Override
170 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Romain Guy9c957372011-01-04 17:39:43 -0800171 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 Project9066cfe2009-03-03 19:31:44 -0800177
178 int maxHeight = 0;
179 int maxWidth = 0;
Dianne Hackborn189ee182010-12-02 21:48:53 -0800180 int childState = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 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 Powell2b6be702011-01-08 16:44:07 -0800186 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 Hackborn189ee182010-12-02 21:48:53 -0800191 childState = combineMeasuredStates(childState, child.getMeasuredState());
Romain Guy9c957372011-01-04 17:39:43 -0800192 if (measureMatchParentChildren) {
Romain Guy9c957372011-01-04 17:39:43 -0800193 if (lp.width == LayoutParams.MATCH_PARENT ||
194 lp.height == LayoutParams.MATCH_PARENT) {
195 mMatchParentChildren.add(child);
196 }
197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 }
199 }
200
201 // Account for padding too
Michael Jurka02473da2011-09-08 18:26:23 -0700202 maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
203 maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
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 Hackborn189ee182010-12-02 21:48:53 -0800216 setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
217 resolveSizeAndState(maxHeight, heightMeasureSpec,
Romain Guy9c957372011-01-04 17:39:43 -0800218 childState << MEASURED_HEIGHT_STATE_SHIFT));
219
Romain Guya174d7a2011-01-07 13:27:39 -0800220 count = mMatchParentChildren.size();
221 if (count > 1) {
Romain Guy9c957372011-01-04 17:39:43 -0800222 for (int i = 0; i < count; i++) {
223 final View child = mMatchParentChildren.get(i);
Romain Guy9c957372011-01-04 17:39:43 -0800224 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
Alan Viverette39fd9022015-06-25 12:24:27 -0700225
226 final int childWidthMeasureSpec;
Romain Guy9c957372011-01-04 17:39:43 -0800227 if (lp.width == LayoutParams.MATCH_PARENT) {
Alan Viverette39fd9022015-06-25 12:24:27 -0700228 final int width = Math.max(0, getMeasuredWidth()
229 - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
230 - lp.leftMargin - lp.rightMargin);
231 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
232 width, MeasureSpec.EXACTLY);
Romain Guy9c957372011-01-04 17:39:43 -0800233 } else {
234 childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
Michael Jurka02473da2011-09-08 18:26:23 -0700235 getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
236 lp.leftMargin + lp.rightMargin,
Romain Guy9c957372011-01-04 17:39:43 -0800237 lp.width);
238 }
Alan Viverette39fd9022015-06-25 12:24:27 -0700239
240 final int childHeightMeasureSpec;
Romain Guy9c957372011-01-04 17:39:43 -0800241 if (lp.height == LayoutParams.MATCH_PARENT) {
Alan Viverette39fd9022015-06-25 12:24:27 -0700242 final int height = Math.max(0, getMeasuredHeight()
243 - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
244 - lp.topMargin - lp.bottomMargin);
245 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
246 height, MeasureSpec.EXACTLY);
Romain Guy9c957372011-01-04 17:39:43 -0800247 } else {
Romain Guycf70dcb2011-01-07 11:03:20 -0800248 childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
Michael Jurka02473da2011-09-08 18:26:23 -0700249 getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
250 lp.topMargin + lp.bottomMargin,
Romain Guy9c957372011-01-04 17:39:43 -0800251 lp.height);
252 }
253
254 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
255 }
256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Alan Viverettebd53c982016-03-31 16:55:06 -0400258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 @Override
260 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Fabrice Di Meglioc4d71222013-06-11 19:13:54 -0700261 layoutChildren(left, top, right, bottom, false /* no force left gravity */);
262 }
263
Alan Viverettebd53c982016-03-31 16:55:06 -0400264 void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 final int count = getChildCount();
266
Michael Jurka02473da2011-09-08 18:26:23 -0700267 final int parentLeft = getPaddingLeftWithForeground();
268 final int parentRight = right - left - getPaddingRightWithForeground();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Michael Jurka02473da2011-09-08 18:26:23 -0700270 final int parentTop = getPaddingTopWithForeground();
271 final int parentBottom = bottom - top - getPaddingBottomWithForeground();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272
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 Guy9c957372011-01-04 17:39:43 -0800281 int childLeft;
282 int childTop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
Adam Powell5e0ae672010-12-06 21:33:04 -0800284 int gravity = lp.gravity;
285 if (gravity == -1) {
286 gravity = DEFAULT_CHILD_GRAVITY;
287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -0700289 final int layoutDirection = getLayoutDirection();
Fabrice Di Meglioc0053222011-06-13 12:16:51 -0700290 final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
Adam Powell5e0ae672010-12-06 21:33:04 -0800291 final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
Fabrice Di Megliode35cee2011-06-01 15:13:50 -0700293 switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
Adam Powell5e0ae672010-12-06 21:33:04 -0800294 case Gravity.CENTER_HORIZONTAL:
295 childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
296 lp.leftMargin - lp.rightMargin;
297 break;
298 case Gravity.RIGHT:
Fabrice Di Meglioc4d71222013-06-11 19:13:54 -0700299 if (!forceLeftGravity) {
300 childLeft = parentRight - width - lp.rightMargin;
301 break;
302 }
303 case Gravity.LEFT:
Adam Powell5e0ae672010-12-06 21:33:04 -0800304 default:
305 childLeft = parentLeft + lp.leftMargin;
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Adam Powell5e0ae672010-12-06 21:33:04 -0800308 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 Project9066cfe2009-03-03 19:31:44 -0800321 }
322
323 child.layout(childLeft, childTop, childLeft + width, childTop + height);
324 }
325 }
326 }
327
328 /**
Steve Blockb25825a2011-04-27 14:51:38 +0100329 * 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 Project9066cfe2009-03-03 19:31:44 -0800332 * @param measureAll true to consider children marked GONE, false otherwise.
333 * Default value is false.
Steve Blockb25825a2011-04-27 14:51:38 +0100334 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 * @attr ref android.R.styleable#FrameLayout_measureAllChildren
336 */
337 @android.view.RemotableViewMethod
338 public void setMeasureAllChildren(boolean measureAll) {
339 mMeasureAllChildren = measureAll;
340 }
Steve Blockb25825a2011-04-27 14:51:38 +0100341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 /**
Steve Blockb25825a2011-04-27 14:51:38 +0100343 * 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 Project9066cfe2009-03-03 19:31:44 -0800352 */
Steve Blockb25825a2011-04-27 14:51:38 +0100353 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 public boolean getConsiderGoneChildrenWhenMeasuring() {
Steve Blockb25825a2011-04-27 14:51:38 +0100355 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 Project9066cfe2009-03-03 19:31:44 -0800365 return mMeasureAllChildren;
366 }
367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 @Override
369 public LayoutParams generateLayoutParams(AttributeSet attrs) {
Alan Viverettebd53c982016-03-31 16:55:06 -0400370 return new FrameLayout.LayoutParams(getContext(), attrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
372
Patrick Dubroye0a799a2011-05-04 16:19:22 -0700373 @Override
374 public boolean shouldDelayChildPressedState() {
375 return false;
376 }
377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 @Override
379 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
380 return p instanceof LayoutParams;
381 }
382
383 @Override
Yigit Boyar885c50b2016-03-22 16:53:42 -0700384 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 Project9066cfe2009-03-03 19:31:44 -0800392 }
393
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800394 @Override
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800395 public CharSequence getAccessibilityClassName() {
396 return FrameLayout.class.getName();
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800397 }
398
Siva Velusamy94a6d152015-05-05 15:07:00 -0700399 /** @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 Project9066cfe2009-03-03 19:31:44 -0800411 /**
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 Viverettebd53c982016-03-31 16:55:06 -0400415 *
Romain Guy606e8cc2010-08-17 12:43:05 -0700416 * @attr ref android.R.styleable#FrameLayout_Layout_layout_gravity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 */
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 Viverettebd53c982016-03-31 16:55:06 -0400422 * <p>
423 * The default value is {@code Gravity.TOP | Gravity.START}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 *
425 * @see android.view.Gravity
Romain Guy606e8cc2010-08-17 12:43:05 -0700426 * @attr ref android.R.styleable#FrameLayout_Layout_layout_gravity
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 */
Alan Viverettebd53c982016-03-31 16:55:06 -0400428 public int gravity = DEFAULT_CHILD_GRAVITY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
Alan Viverettebd53c982016-03-31 16:55:06 -0400430 public LayoutParams(@NonNull Context c, @Nullable AttributeSet attrs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 super(c, attrs);
432
Alan Viverettebd53c982016-03-31 16:55:06 -0400433 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 Project9066cfe2009-03-03 19:31:44 -0800436 a.recycle();
437 }
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 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 Guy980a9382010-01-08 15:06:28 -0800447 * @param width the width, either {@link #MATCH_PARENT},
Alan Viverettebd53c982016-03-31 16:55:06 -0400448 * {@link #WRAP_CONTENT} or a fixed size in pixels
Romain Guy980a9382010-01-08 15:06:28 -0800449 * @param height the height, either {@link #MATCH_PARENT},
Alan Viverettebd53c982016-03-31 16:55:06 -0400450 * {@link #WRAP_CONTENT} or a fixed size in pixels
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 * @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 Viverettebd53c982016-03-31 16:55:06 -0400460 public LayoutParams(@NonNull ViewGroup.LayoutParams source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 super(source);
462 }
463
Alan Viverettebd53c982016-03-31 16:55:06 -0400464 public LayoutParams(@NonNull ViewGroup.MarginLayoutParams source) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 super(source);
466 }
Alan Viverette0a0e1552013-08-07 13:24:09 -0700467
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 Viverettebd53c982016-03-31 16:55:06 -0400474 public LayoutParams(@NonNull LayoutParams source) {
Alan Viverette0a0e1552013-08-07 13:24:09 -0700475 super(source);
476
477 this.gravity = source.gravity;
478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480}