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;
24 import android.view.inspector.InspectableProperty;
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 {
AbsoluteLayout(Context context)43     public AbsoluteLayout(Context context) {
44         this(context, null);
45     }
46 
AbsoluteLayout(Context context, AttributeSet attrs)47     public AbsoluteLayout(Context context, AttributeSet attrs) {
48         this(context, attrs, 0);
49     }
50 
AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr)51     public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr) {
52         this(context, attrs, defStyleAttr, 0);
53     }
54 
AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)55     public AbsoluteLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57     }
58 
59     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)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 
95         setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
96                 resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
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
generateDefaultLayoutParams()106     protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
107         return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
108     }
109 
110     @Override
onLayout(boolean changed, int l, int t, int r, int b)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
generateLayoutParams(AttributeSet attrs)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
checkLayoutParams(ViewGroup.LayoutParams p)139     protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
140         return p instanceof AbsoluteLayout.LayoutParams;
141     }
142 
143     @Override
generateLayoutParams(ViewGroup.LayoutParams p)144     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
145         return new LayoutParams(p);
146     }
147 
148     @Override
shouldDelayChildPressedState()149     public boolean shouldDelayChildPressedState() {
150         return false;
151     }
152 
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          */
163         @InspectableProperty(name = "layout_x")
164         public int x;
165         /**
166          * The vertical, or Y, location of the child within the view group.
167          */
168         @InspectableProperty(name = "layout_y")
169         public int y;
170 
171         /**
172          * Creates a new set of layout parameters with the specified width,
173          * height and location.
174          *
175          * @param width the width, either {@link #MATCH_PARENT},
176                   {@link #WRAP_CONTENT} or a fixed size in pixels
177          * @param height the height, either {@link #MATCH_PARENT},
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          */
LayoutParams(int width, int height, int x, int y)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
201          * @param attrs the set of attributes from which to extract the layout
202          *              parameters values
203          */
LayoutParams(Context c, AttributeSet attrs)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          */
LayoutParams(ViewGroup.LayoutParams source)218         public LayoutParams(ViewGroup.LayoutParams source) {
219             super(source);
220         }
221 
222         @Override
debug(String output)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 
232