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