Home | History | Annotate | Download | only in widget
      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.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
     41 public 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(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
     92                 resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
     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     @Override
    145     public boolean shouldDelayChildPressedState() {
    146         return false;
    147     }
    148 
    149     /**
    150      * Per-child layout information associated with AbsoluteLayout.
    151      * See
    152      * {@link android.R.styleable#AbsoluteLayout_Layout Absolute Layout Attributes}
    153      * for a list of all child view attributes that this class supports.
    154      */
    155     public static class LayoutParams extends ViewGroup.LayoutParams {
    156         /**
    157          * The horizontal, or X, location of the child within the view group.
    158          */
    159         public int x;
    160         /**
    161          * The vertical, or Y, location of the child within the view group.
    162          */
    163         public int y;
    164 
    165         /**
    166          * Creates a new set of layout parameters with the specified width,
    167          * height and location.
    168          *
    169          * @param width the width, either {@link #MATCH_PARENT},
    170                   {@link #WRAP_CONTENT} or a fixed size in pixels
    171          * @param height the height, either {@link #MATCH_PARENT},
    172                   {@link #WRAP_CONTENT} or a fixed size in pixels
    173          * @param x the X location of the child
    174          * @param y the Y location of the child
    175          */
    176         public LayoutParams(int width, int height, int x, int y) {
    177             super(width, height);
    178             this.x = x;
    179             this.y = y;
    180         }
    181 
    182         /**
    183          * Creates a new set of layout parameters. The values are extracted from
    184          * the supplied attributes set and context. The XML attributes mapped
    185          * to this set of layout parameters are:
    186          *
    187          * <ul>
    188          *   <li><code>layout_x</code>: the X location of the child</li>
    189          *   <li><code>layout_y</code>: the Y location of the child</li>
    190          *   <li>All the XML attributes from
    191          *   {@link android.view.ViewGroup.LayoutParams}</li>
    192          * </ul>
    193          *
    194          * @param c the application environment
    195          * @param attrs the set of attributes from which to extract the layout
    196          *              parameters values
    197          */
    198         public LayoutParams(Context c, AttributeSet attrs) {
    199             super(c, attrs);
    200             TypedArray a = c.obtainStyledAttributes(attrs,
    201                     com.android.internal.R.styleable.AbsoluteLayout_Layout);
    202             x = a.getDimensionPixelOffset(
    203                     com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_x, 0);
    204             y = a.getDimensionPixelOffset(
    205                     com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_y, 0);
    206             a.recycle();
    207         }
    208 
    209         /**
    210          * {@inheritDoc}
    211          */
    212         public LayoutParams(ViewGroup.LayoutParams source) {
    213             super(source);
    214         }
    215 
    216         @Override
    217         public String debug(String output) {
    218             return output + "Absolute.LayoutParams={width="
    219                     + sizeToString(width) + ", height=" + sizeToString(height)
    220                     + " x=" + x + " y=" + y + "}";
    221         }
    222     }
    223 }
    224 
    225 
    226