FileDocCategorySizeDatePackage
FrameLayout.javaAPI DocAndroid 1.5 API17378Wed May 06 22:41:56 BST 2009android.widget

FrameLayout

public class FrameLayout extends android.view.ViewGroup
FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if {@link #setMeasureAllChildren(boolean) setConsiderGoneChildrenWhenMeasuring()} is set to true.
attr
ref android.R.styleable#FrameLayout_foreground
attr
ref android.R.styleable#FrameLayout_foregroundGravity
attr
ref android.R.styleable#FrameLayout_measureAllChildren

Fields Summary
boolean
mMeasureAllChildren
private android.graphics.drawable.Drawable
mForeground
private int
mForegroundPaddingLeft
private int
mForegroundPaddingTop
private int
mForegroundPaddingRight
private int
mForegroundPaddingBottom
private final android.graphics.Rect
mSelfBounds
private final android.graphics.Rect
mOverlayBounds
private int
mForegroundGravity
protected boolean
mForegroundInPadding
{@hide}
boolean
mForegroundBoundsChanged
Constructors Summary
public FrameLayout(android.content.Context context)

    
       
        super(context);
    
public FrameLayout(android.content.Context context, android.util.AttributeSet attrs)

        this(context, attrs, 0);
    
public FrameLayout(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.FrameLayout,
                    defStyle, 0);

        mForegroundGravity = a.getInt(
                com.android.internal.R.styleable.FrameLayout_foregroundGravity, mForegroundGravity);

        final Drawable d = a.getDrawable(com.android.internal.R.styleable.FrameLayout_foreground);
        if (d != null) {
            setForeground(d);
        }
        
        if (a.getBoolean(com.android.internal.R.styleable.FrameLayout_measureAllChildren, false)) {
            setMeasureAllChildren(true);
        }

        mForegroundInPadding = a.getBoolean(
                com.android.internal.R.styleable.FrameLayout_foregroundInsidePadding, true);

        a.recycle();
    
Methods Summary
protected booleancheckLayoutParams(ViewGroup.LayoutParams p)
{@inheritDoc}

        return p instanceof LayoutParams;
    
public voiddraw(android.graphics.Canvas canvas)
{@inheritDoc}

        super.draw(canvas);

        if (mForeground != null) {
            final Drawable foreground = mForeground;
            if (mForegroundBoundsChanged) {
                mForegroundBoundsChanged = false;
                if (foreground != null) {
                    final Rect selfBounds = mSelfBounds;
                    final Rect overlayBounds = mOverlayBounds;

                    final int w = mRight-mLeft;
                    final int h = mBottom-mTop;
                    
                    if (mForegroundInPadding) {
                        selfBounds.set(0, 0, w, h);
                    } else {
                        selfBounds.set(mPaddingLeft, mPaddingTop, w - mPaddingRight, h - mPaddingBottom);
                    }

                    Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),
                            foreground.getIntrinsicHeight(), selfBounds, overlayBounds);
                    foreground.setBounds(overlayBounds);
                }
            }
            
            foreground.draw(canvas);
        }
    
protected voiddrawableStateChanged()
{@inheritDoc}

        super.drawableStateChanged();
        if (mForeground != null && mForeground.isStateful()) {
            mForeground.setState(getDrawableState());
        }
    
public booleangatherTransparentRegion(android.graphics.Region region)
{@inheritDoc}

        boolean opaque = super.gatherTransparentRegion(region);
        if (region != null && mForeground != null) {
            applyDrawableToTransparentRegion(mForeground, region);
        }
        return opaque;
    
protected android.widget.FrameLayout$LayoutParamsgenerateDefaultLayoutParams()
Returns a set of layout parameters with a width of {@link android.view.ViewGroup.LayoutParams#FILL_PARENT}, and a height of {@link android.view.ViewGroup.LayoutParams#FILL_PARENT}.

        return new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    
public android.widget.FrameLayout$LayoutParamsgenerateLayoutParams(android.util.AttributeSet attrs)
{@inheritDoc}

        return new FrameLayout.LayoutParams(getContext(), attrs);        
    
protected ViewGroup.LayoutParamsgenerateLayoutParams(ViewGroup.LayoutParams p)

        return new LayoutParams(p);
    
public booleangetConsiderGoneChildrenWhenMeasuring()
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring.

        return mMeasureAllChildren;
    
public android.graphics.drawable.DrawablegetForeground()
Returns the drawable used as the foreground of this FrameLayout. The foreground drawable, if non-null, is always drawn on top of the children.

return
A Drawable or null if no foreground was set.

        return mForeground;
    
protected voidonLayout(boolean changed, int left, int top, int right, int bottom)
{@inheritDoc}

        final int count = getChildCount();

        final int parentLeft = mPaddingLeft + mForegroundPaddingLeft;
        final int parentRight = right - left - mPaddingRight - mForegroundPaddingRight;

        final int parentTop = mPaddingTop + mForegroundPaddingTop;
        final int parentBottom = bottom - top - mPaddingBottom - mForegroundPaddingBottom;

        mForegroundBoundsChanged = true;
        
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();

                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();

                int childLeft = parentLeft;
                int childTop = parentTop;

                final int gravity = lp.gravity;

                if (gravity != -1) {
                    final int horizontalGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                    final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

                    switch (horizontalGravity) {
                        case Gravity.LEFT:
                            childLeft = parentLeft + lp.leftMargin;
                            break;
                        case Gravity.CENTER_HORIZONTAL:
                            childLeft = parentLeft + (parentRight - parentLeft + lp.leftMargin +
                                    lp.rightMargin - width) / 2;
                            break;
                        case Gravity.RIGHT:
                            childLeft = parentRight - width - lp.rightMargin;
                            break;
                        default:
                            childLeft = parentLeft + lp.leftMargin;
                    }

                    switch (verticalGravity) {
                        case Gravity.TOP:
                            childTop = parentTop + lp.topMargin;
                            break;
                        case Gravity.CENTER_VERTICAL:
                            childTop = parentTop + (parentBottom - parentTop + lp.topMargin +
                                    lp.bottomMargin - height) / 2;
                            break;
                        case Gravity.BOTTOM:
                            childTop = parentBottom - height - lp.bottomMargin;
                            break;
                        default:
                            childTop = parentTop + lp.topMargin;
                    }
                }

                child.layout(childLeft, childTop, childLeft + width, childTop + height);
            }
        }
    
protected voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)
{@inheritDoc}

        final int count = getChildCount();

        int maxHeight = 0;
        int maxWidth = 0;

        // Find rightmost and bottommost child
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (mMeasureAllChildren || child.getVisibility() != GONE) {
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
            }
        }

        // Account for padding too
        maxWidth += mPaddingLeft + mPaddingRight + mForegroundPaddingLeft + mForegroundPaddingRight;
        maxHeight += mPaddingTop + mPaddingBottom + mForegroundPaddingTop + mForegroundPaddingBottom;

        // Check against our minimum height and width
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());

        // Check against our foreground's minimum height and width
        final Drawable drawable = getForeground();
        if (drawable != null) {
            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
        }

        setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
                resolveSize(maxHeight, heightMeasureSpec));
    
protected voidonSizeChanged(int w, int h, int oldw, int oldh)
{@inheritDoc}

        super.onSizeChanged(w, h, oldw, oldh);
        mForegroundBoundsChanged = true;
    
public voidsetForeground(android.graphics.drawable.Drawable drawable)
Supply a Drawable that is to be rendered on top of all of the child views in the frame layout. Any padding in the Drawable will be taken into account by ensuring that the children are inset to be placed inside of the padding area.

param
drawable The Drawable to be drawn on top of the children.
attr
ref android.R.styleable#FrameLayout_foreground

        if (mForeground != drawable) {
            if (mForeground != null) {
                mForeground.setCallback(null);
                unscheduleDrawable(mForeground);
            }

            mForeground = drawable;
            mForegroundPaddingLeft = 0;
            mForegroundPaddingTop = 0;
            mForegroundPaddingRight = 0;
            mForegroundPaddingBottom = 0;

            if (drawable != null) {
                setWillNotDraw(false);
                drawable.setCallback(this);
                if (drawable.isStateful()) {
                    drawable.setState(getDrawableState());
                }
                if (mForegroundGravity == Gravity.FILL) {
                    Rect padding = new Rect();
                    if (drawable.getPadding(padding)) {
                        mForegroundPaddingLeft = padding.left;
                        mForegroundPaddingTop = padding.top;
                        mForegroundPaddingRight = padding.right;
                        mForegroundPaddingBottom = padding.bottom;
                    }
                }
            }  else {
                setWillNotDraw(true);
            }
            requestLayout();
            invalidate();
        }
    
public voidsetForegroundGravity(int foregroundGravity)
Describes how the foreground is positioned. Defaults to FILL.

param
foregroundGravity See {@link android.view.Gravity}
attr
ref android.R.styleable#FrameLayout_foregroundGravity

        if (mForegroundGravity != foregroundGravity) {
            if ((foregroundGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.LEFT;
            }

            if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.TOP;
            }

            mForegroundGravity = foregroundGravity;


            if (mForegroundGravity == Gravity.FILL && mForeground != null) {
                Rect padding = new Rect();
                if (mForeground.getPadding(padding)) {
                    mForegroundPaddingLeft = padding.left;
                    mForegroundPaddingTop = padding.top;
                    mForegroundPaddingRight = padding.right;
                    mForegroundPaddingBottom = padding.bottom;
                }
            } else {
                mForegroundPaddingLeft = 0;
                mForegroundPaddingTop = 0;
                mForegroundPaddingRight = 0;
                mForegroundPaddingBottom = 0;
            }

            requestLayout();
        }
    
public voidsetMeasureAllChildren(boolean measureAll)
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.

param
measureAll true to consider children marked GONE, false otherwise. Default value is false.
attr
ref android.R.styleable#FrameLayout_measureAllChildren

        mMeasureAllChildren = measureAll;
    
protected booleanverifyDrawable(android.graphics.drawable.Drawable who)
{@inheritDoc}

        return super.verifyDrawable(who) || (who == mForeground);