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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 24 | import android.view.inspector.InspectableProperty; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.widget.RemoteViews.RemoteView; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * A layout that lets you specify exact locations (x/y coordinates) of its |
| 30 | * children. Absolute layouts are less flexible and harder to maintain than |
| 31 | * other types of layouts without absolute positioning. |
| 32 | * |
| 33 | * <p><strong>XML attributes</strong></p> <p> See {@link |
| 34 | * android.R.styleable#ViewGroup ViewGroup Attributes}, {@link |
| 35 | * android.R.styleable#View View Attributes}</p> |
| 36 | * |
| 37 | * @deprecated Use {@link android.widget.FrameLayout}, {@link android.widget.RelativeLayout} |
| 38 | * or a custom layout instead. |
| 39 | */ |
| 40 | @Deprecated |
| 41 | @RemoteView |
| 42 | public class AbsoluteLayout extends ViewGroup { |
| 43 | public AbsoluteLayout(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 44 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public AbsoluteLayout(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 48 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 51 | public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 52 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 56 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 61 | int count = getChildCount(); |
| 62 | |
| 63 | int maxHeight = 0; |
| 64 | int maxWidth = 0; |
| 65 | |
| 66 | // Find out how big everyone wants to be |
| 67 | measureChildren(widthMeasureSpec, heightMeasureSpec); |
| 68 | |
| 69 | // Find rightmost and bottom-most child |
| 70 | for (int i = 0; i < count; i++) { |
| 71 | View child = getChildAt(i); |
| 72 | if (child.getVisibility() != GONE) { |
| 73 | int childRight; |
| 74 | int childBottom; |
| 75 | |
| 76 | AbsoluteLayout.LayoutParams lp |
| 77 | = (AbsoluteLayout.LayoutParams) child.getLayoutParams(); |
| 78 | |
| 79 | childRight = lp.x + child.getMeasuredWidth(); |
| 80 | childBottom = lp.y + child.getMeasuredHeight(); |
| 81 | |
| 82 | maxWidth = Math.max(maxWidth, childRight); |
| 83 | maxHeight = Math.max(maxHeight, childBottom); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Account for padding too |
| 88 | maxWidth += mPaddingLeft + mPaddingRight; |
| 89 | maxHeight += mPaddingTop + mPaddingBottom; |
| 90 | |
| 91 | // Check against minimum height and width |
| 92 | maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); |
| 93 | maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); |
| 94 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 95 | setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0), |
| 96 | resolveSizeAndState(maxHeight, heightMeasureSpec, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a set of layout parameters with a width of |
| 101 | * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}, |
| 102 | * a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} |
| 103 | * and with the coordinates (0, 0). |
| 104 | */ |
| 105 | @Override |
| 106 | protected ViewGroup.LayoutParams generateDefaultLayoutParams() { |
| 107 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onLayout(boolean changed, int l, int t, |
| 112 | int r, int b) { |
| 113 | int count = getChildCount(); |
| 114 | |
| 115 | for (int i = 0; i < count; i++) { |
| 116 | View child = getChildAt(i); |
| 117 | if (child.getVisibility() != GONE) { |
| 118 | |
| 119 | AbsoluteLayout.LayoutParams lp = |
| 120 | (AbsoluteLayout.LayoutParams) child.getLayoutParams(); |
| 121 | |
| 122 | int childLeft = mPaddingLeft + lp.x; |
| 123 | int childTop = mPaddingTop + lp.y; |
| 124 | child.layout(childLeft, childTop, |
| 125 | childLeft + child.getMeasuredWidth(), |
| 126 | childTop + child.getMeasuredHeight()); |
| 127 | |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 134 | return new AbsoluteLayout.LayoutParams(getContext(), attrs); |
| 135 | } |
| 136 | |
| 137 | // Override to allow type-checking of LayoutParams. |
| 138 | @Override |
| 139 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 140 | return p instanceof AbsoluteLayout.LayoutParams; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 145 | return new LayoutParams(p); |
| 146 | } |
| 147 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 148 | @Override |
| 149 | public boolean shouldDelayChildPressedState() { |
| 150 | return false; |
| 151 | } |
| 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Per-child layout information associated with AbsoluteLayout. |
| 155 | * See |
| 156 | * {@link android.R.styleable#AbsoluteLayout_Layout Absolute Layout Attributes} |
| 157 | * for a list of all child view attributes that this class supports. |
| 158 | */ |
| 159 | public static class LayoutParams extends ViewGroup.LayoutParams { |
| 160 | /** |
| 161 | * The horizontal, or X, location of the child within the view group. |
| 162 | */ |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 163 | @InspectableProperty(name = "layout_x") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | public int x; |
| 165 | /** |
| 166 | * The vertical, or Y, location of the child within the view group. |
| 167 | */ |
Ashley Rose | de3b4a7 | 2019-03-04 16:44:07 -0500 | [diff] [blame] | 168 | @InspectableProperty(name = "layout_y") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | public int y; |
| 170 | |
| 171 | /** |
| 172 | * Creates a new set of layout parameters with the specified width, |
| 173 | * height and location. |
| 174 | * |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 175 | * @param width the width, either {@link #MATCH_PARENT}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | {@link #WRAP_CONTENT} or a fixed size in pixels |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 177 | * @param height the height, either {@link #MATCH_PARENT}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | {@link #WRAP_CONTENT} or a fixed size in pixels |
| 179 | * @param x the X location of the child |
| 180 | * @param y the Y location of the child |
| 181 | */ |
| 182 | public LayoutParams(int width, int height, int x, int y) { |
| 183 | super(width, height); |
| 184 | this.x = x; |
| 185 | this.y = y; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Creates a new set of layout parameters. The values are extracted from |
| 190 | * the supplied attributes set and context. The XML attributes mapped |
| 191 | * to this set of layout parameters are: |
| 192 | * |
| 193 | * <ul> |
| 194 | * <li><code>layout_x</code>: the X location of the child</li> |
| 195 | * <li><code>layout_y</code>: the Y location of the child</li> |
| 196 | * <li>All the XML attributes from |
| 197 | * {@link android.view.ViewGroup.LayoutParams}</li> |
| 198 | * </ul> |
| 199 | * |
| 200 | * @param c the application environment |
Uwe Voelker | fc8daba | 2010-10-31 10:08:04 +0100 | [diff] [blame] | 201 | * @param attrs the set of attributes from which to extract the layout |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | * parameters values |
| 203 | */ |
| 204 | public LayoutParams(Context c, AttributeSet attrs) { |
| 205 | super(c, attrs); |
| 206 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 207 | com.android.internal.R.styleable.AbsoluteLayout_Layout); |
| 208 | x = a.getDimensionPixelOffset( |
| 209 | com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_x, 0); |
| 210 | y = a.getDimensionPixelOffset( |
| 211 | com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_y, 0); |
| 212 | a.recycle(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * {@inheritDoc} |
| 217 | */ |
| 218 | public LayoutParams(ViewGroup.LayoutParams source) { |
| 219 | super(source); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public String debug(String output) { |
| 224 | return output + "Absolute.LayoutParams={width=" |
| 225 | + sizeToString(width) + ", height=" + sizeToString(height) |
| 226 | + " x=" + x + " y=" + y + "}"; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | |