2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android
.widget
;
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
.widget
.RemoteViews
.RemoteView
;
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.
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>
36 * @deprecated Use {@link android.widget.FrameLayout}, {@link android.widget.RelativeLayout}
37 * or a custom layout instead.
41 public class AbsoluteLayout
extends ViewGroup
{
42 public AbsoluteLayout(Context context
) {
46 public AbsoluteLayout(Context context
, AttributeSet attrs
) {
47 this(context
, attrs
, 0);
50 public AbsoluteLayout(Context context
, AttributeSet attrs
, int defStyleAttr
) {
51 this(context
, attrs
, defStyleAttr
, 0);
54 public AbsoluteLayout(Context context
, AttributeSet attrs
, int defStyleAttr
, int defStyleRes
) {
55 super(context
, attrs
, defStyleAttr
, defStyleRes
);
59 protected void onMeasure(int widthMeasureSpec
, int heightMeasureSpec
) {
60 int count
= getChildCount();
65 // Find out how big everyone wants to be
66 measureChildren(widthMeasureSpec
, heightMeasureSpec
);
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
) {
75 AbsoluteLayout
.LayoutParams lp
76 = (AbsoluteLayout
.LayoutParams
) child
.getLayoutParams();
78 childRight
= lp
.x
+ child
.getMeasuredWidth();
79 childBottom
= lp
.y
+ child
.getMeasuredHeight();
81 maxWidth
= Math
.max(maxWidth
, childRight
);
82 maxHeight
= Math
.max(maxHeight
, childBottom
);
86 // Account for padding too
87 maxWidth
+= mPaddingLeft
+ mPaddingRight
;
88 maxHeight
+= mPaddingTop
+ mPaddingBottom
;
90 // Check against minimum height and width
91 maxHeight
= Math
.max(maxHeight
, getSuggestedMinimumHeight());
92 maxWidth
= Math
.max(maxWidth
, getSuggestedMinimumWidth());
94 setMeasuredDimension(resolveSizeAndState(maxWidth
, widthMeasureSpec
, 0),
95 resolveSizeAndState(maxHeight
, heightMeasureSpec
, 0));
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).
105 protected ViewGroup
.LayoutParams
generateDefaultLayoutParams() {
106 return new LayoutParams(LayoutParams
.WRAP_CONTENT
, LayoutParams
.WRAP_CONTENT
, 0, 0);
110 protected void onLayout(boolean changed
, int l
, int t
,
112 int count
= getChildCount();
114 for (int i
= 0; i
< count
; i
++) {
115 View child
= getChildAt(i
);
116 if (child
.getVisibility() != GONE
) {
118 AbsoluteLayout
.LayoutParams lp
=
119 (AbsoluteLayout
.LayoutParams
) child
.getLayoutParams();
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());
132 public ViewGroup
.LayoutParams
generateLayoutParams(AttributeSet attrs
) {
133 return new AbsoluteLayout
.LayoutParams(getContext(), attrs
);
136 // Override to allow type-checking of LayoutParams.
138 protected boolean checkLayoutParams(ViewGroup
.LayoutParams p
) {
139 return p
instanceof AbsoluteLayout
.LayoutParams
;
143 protected ViewGroup
.LayoutParams
generateLayoutParams(ViewGroup
.LayoutParams p
) {
144 return new LayoutParams(p
);
148 public boolean shouldDelayChildPressedState() {
153 * Per-child layout information associated with AbsoluteLayout.
155 * {@link android.R.styleable#AbsoluteLayout_Layout Absolute Layout Attributes}
156 * for a list of all child view attributes that this class supports.
158 public static class LayoutParams
extends ViewGroup
.LayoutParams
{
160 * The horizontal, or X, location of the child within the view group.
164 * The vertical, or Y, location of the child within the view group.
169 * Creates a new set of layout parameters with the specified width,
170 * height and location.
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
179 public LayoutParams(int width
, int height
, int x
, int y
) {
180 super(width
, height
);
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:
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>
197 * @param c the application environment
198 * @param attrs the set of attributes from which to extract the layout
201 public LayoutParams(Context c
, AttributeSet 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);
215 public LayoutParams(ViewGroup
.LayoutParams source
) {
220 public String
debug(String output
) {
221 return output
+ "Absolute.LayoutParams={width="
222 + sizeToString(width
) + ", height=" + sizeToString(height
)
223 + " x=" + x
+ " y=" + y
+ "}";