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