Example usage for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK

List of usage examples for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK

Introduction

In this page you can find the example usage for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK.

Prototype

int RELATIVE_HORIZONTAL_GRAVITY_MASK

To view the source code for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK.

Click Source Link

Usage

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * Show the popup list. If the list is already showing, this method
 * will recalculate the popup's size and position.
 *///from www . j  a v a 2s  . co  m
public void show() {
    final int height = buildDropDown();
    final int widthSpec = getListWidthSpec();

    boolean noInputMethod = isInputMethodNotNeeded();
    PopupWindowCompat.setWindowLayoutType(mPopup, mDropDownWindowLayoutType);

    final int marginsLeft = mMargins.left;
    final int marginsTop = mMargins.top;
    final int marginsBottom = mMargins.bottom;
    final int marginsRight = mMargins.right;

    getBackgroundPadding(mTempRect);
    final int backgroundLeft = mTempRect.left;
    final int backgroundTop = mTempRect.top;
    final int backgroundBottom = mTempRect.bottom;
    final int backgroundRight = mTempRect.right;

    int verticalOffset = mDropDownVerticalOffset;
    int horizontalOffset = mDropDownHorizontalOffset;

    final int anchorWidth = mDropDownAnchorView.getWidth();
    final int anchorHeight = mDropDownAnchorView.getHeight();

    getLocationInWindow(mDropDownAnchorView, mTempLocation);
    final int anchorLeft = mTempLocation[0];
    final int anchorRight = anchorLeft + anchorWidth;
    final int anchorTop = mTempLocation[1];
    final int anchorBottom = anchorTop + anchorHeight;

    final boolean rightAligned = GravityCompat.getAbsoluteGravity(
            getDropDownGravity() & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK,
            mLayoutDirection) == Gravity.RIGHT;
    if (rightAligned) {
        horizontalOffset += anchorWidth - widthSpec - (marginsRight - backgroundRight);
    } else {
        horizontalOffset += (marginsLeft - backgroundLeft);
    }

    final int bottomDecorations = getWindowFrame(mDropDownAnchorView, noInputMethod, mTempRect);
    final int windowLeft = mTempRect.left;
    final int windowRight = mTempRect.right;
    final int windowTop = mTempRect.top;
    final int windowBottom = mTempRect.bottom;

    final int windowWidth = windowRight - windowLeft;
    final int windowHeight = windowBottom - windowTop;

    getBoundsInWindow(mTempRect);
    final int boundsTop = mTempRect.top;
    final int boundsRight = mTempRect.right;
    final int boundsLeft = mTempRect.left;
    final int boundsBottom = mTempRect.bottom;

    final int screenRight = windowRight - (marginsRight - backgroundRight) - boundsRight;
    final int screenLeft = windowLeft + (marginsLeft - backgroundLeft) + boundsLeft;
    final int screenWidth = screenRight - screenLeft;

    if (!rightAligned && windowWidth < anchorLeft + horizontalOffset + widthSpec) {
        // When right aligned due to insufficient space ignore negative horizontal offset.
        horizontalOffset = mDropDownHorizontalOffset < 0 ? 0 : mDropDownHorizontalOffset;
        horizontalOffset -= widthSpec - (windowWidth - anchorLeft);
        horizontalOffset -= marginsRight - backgroundRight;
    } else if (rightAligned && 0 > anchorLeft + horizontalOffset) {
        // When left aligned due to insufficient space ignore positive horizontal offset.
        horizontalOffset = mDropDownHorizontalOffset > 0 ? 0 : mDropDownHorizontalOffset;
        horizontalOffset -= anchorLeft;
        horizontalOffset += marginsLeft - backgroundLeft;
    }

    // Width spec should always be resolved to concrete value. widthSpec > 0;
    if (windowWidth < widthSpec + horizontalOffset + anchorLeft) {
        int diff = Math.abs(windowWidth - (widthSpec + horizontalOffset + anchorLeft));
        horizontalOffset -= diff;
    } else if (0 > anchorLeft + horizontalOffset) {
        int diff = Math.abs(horizontalOffset + anchorLeft);
        horizontalOffset += diff;
    }

    int maxHeight = getMaxAvailableHeight(mDropDownAnchorView, noInputMethod) + backgroundTop
            + backgroundBottom;
    int availableHeight = maxHeight;
    //        availableHeight -= Math.max(0, marginsTop - backgroundTop);
    //        availableHeight -= Math.max(0, marginsBottom - backgroundBottom);
    availableHeight -= marginsTop - backgroundTop;
    availableHeight -= marginsBottom - backgroundBottom;

    int limitHeight = Math.min(windowHeight, availableHeight);

    final int heightSpec;
    if (mPopup.isShowing()) {
        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            // The call to PopupWindow's update method below can accept -1 for any
            // value you do not want to update.
            //                heightSpec = noInputMethod ? height : ViewGroup.LayoutParams.MATCH_PARENT;
            //                if (noInputMethod) {
            //                    mPopup.setWidth(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
            //                        ViewGroup.LayoutParams.MATCH_PARENT : 0);
            //                    mPopup.setHeight(0);
            //                } else {
            //                    mPopup.setWidth(mDropDownWidth == ViewGroup.LayoutParams.MATCH_PARENT ?
            //                        ViewGroup.LayoutParams.MATCH_PARENT : 0);
            //                    mPopup.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            //                }
            heightSpec = limitHeight;
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = Math.min(height, limitHeight);
        } else {
            heightSpec = Math.min(mDropDownHeight, limitHeight);
        }
    } else {
        if (mDropDownHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
            //                heightSpec = ViewGroup.LayoutParams.MATCH_PARENT;
            heightSpec = limitHeight;
        } else if (mDropDownHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
            heightSpec = Math.min(height, limitHeight);
        } else {
            heightSpec = Math.min(mDropDownHeight, limitHeight);
        }
    }

    final int screenBottom = windowBottom - (marginsBottom - backgroundBottom) - boundsBottom;
    final int screenTop = windowTop + (marginsTop - backgroundTop) + boundsTop;

    {
        // Position within bounds.

        final int popupTop = anchorBottom + verticalOffset;
        final int popupBottom = popupTop + heightSpec;
        final int popupHeight = popupBottom - popupTop;

        if (popupBottom > screenBottom) {
            verticalOffset -= (popupBottom - screenBottom);
        } else if (popupTop < screenTop) {
            verticalOffset += (screenTop - popupTop);
        }
    }

    {
        // Account for background padding.

        final int popupTop = anchorBottom + verticalOffset;
        final int popupBottom = popupTop + heightSpec;
        final int popupHeight = popupBottom - popupTop;

        if (windowBottom < popupBottom) {
            int diff = Math.abs(windowBottom - popupBottom);
            verticalOffset -= diff;
        } else if (windowTop > popupTop) {
            int diff = Math.abs(windowTop - popupTop);
            verticalOffset += diff;
        }
    }

    //        verticalOffset -= bottomDecorations;
    //        verticalOffset += Util.dpToPxOffset(mContext, 8);

    if (mPopup.isShowing()) {
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);

        mPopup.update(getAnchorView(), horizontalOffset, verticalOffset, (widthSpec < 0) ? -1 : widthSpec,
                (heightSpec < 0) ? -1 : heightSpec);
    } else {

        mPopup.setWidth(widthSpec);
        mPopup.setHeight(heightSpec);
        setPopupClipToScreenEnabled(true);

        // use outside touchable to dismiss drop down when touching outside of it, so
        // only set this if the dropdown is not always visible
        mPopup.setOutsideTouchable(!mForceIgnoreOutsideTouch && !mDropDownAlwaysVisible);
        mPopup.setTouchInterceptor(mTouchInterceptor);
        if (sSetEpicenterBoundsMethod != null) {
            try {
                sSetEpicenterBoundsMethod.invoke(mPopup, mEpicenterBounds);
            } catch (Exception e) {
                Log.e(TAG, "Could not invoke setEpicenterBounds on PopupWindow", e);
            }
        }
        // We handle gravity manually. Just as everything else.
        PopupWindowCompat.showAsDropDown(mPopup, getAnchorView(), horizontalOffset, verticalOffset,
                Gravity.NO_GRAVITY);
        mDropDownList.setSelection(ListView.INVALID_POSITION);

        if (DEBUG)
            Log.e(TAG, "isAboveAnchor=" + mPopup.isAboveAnchor());

        if (!mModal || mDropDownList.isInTouchMode()) {
            clearListSelection();
        }
        if (!mModal) {
            mHandler.post(mHideSelector);
        }
    }
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

/**
 * Position the children during a layout pass if the orientation of this
 * LinearLayout is set to {@link #VERTICAL}.
 *
 * @see #getOrientation()// w w  w. ja  va2  s  .com
 * @see #setOrientation(int)
 * @see #onLayout(boolean, int, int, int, int)
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
void layoutVertical(int left, int top, int right, int bottom) {
    final int paddingLeft = getPaddingLeft();

    int childTop;
    int childLeft;

    // Where right end of child should go
    final int width = right - left;
    int childRight = width - getPaddingRight();

    // Space available for child
    int childSpace = width - paddingLeft - getPaddingRight();

    final int count = getVirtualChildCount();

    final int majorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
    final int minorGravity = mGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;

    switch (majorGravity) {
    case Gravity.BOTTOM:
        // mTotalLength contains the padding already
        childTop = getPaddingTop() + bottom - top - mTotalLength;
        break;

    // mTotalLength contains the padding already
    case Gravity.CENTER_VERTICAL:
        childTop = getPaddingTop() + (bottom - top - mTotalLength) / 2;
        break;

    case Gravity.TOP:
    default:
        childTop = getPaddingTop();
        break;
    }

    for (int i = 0; i < count; i++) {
        final View child = getVirtualChildAt(i);
        if (child == null) {
            childTop += measureNullChild(i);
        } else if (child.getVisibility() != GONE) {
            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();

            final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child
                    .getLayoutParams();

            int gravity = lp.gravity;
            if (gravity < 0) {
                gravity = minorGravity;
            }
            final int layoutDirection = ViewCompat.getLayoutDirection(this);
            final int absoluteGravity = GravityCompat.getAbsoluteGravity(gravity, layoutDirection);
            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                childLeft = paddingLeft + ((childSpace - childWidth) / 2) + lp.leftMargin - lp.rightMargin;
                break;

            case Gravity.RIGHT:
                childLeft = childRight - childWidth - lp.rightMargin;
                break;

            case Gravity.LEFT:
            default:
                childLeft = paddingLeft + lp.leftMargin;
                break;
            }

            if (hasDividerBeforeChildAt(i)) {
                childTop += mDividerHeight;
            }

            childTop += lp.topMargin;
            setChildFrame(child, childLeft, childTop + getLocationOffset(child), childWidth, childHeight);
            childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);

            i += getChildrenSkipCount(child, i);
        }
    }
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

/**
 * Position the children during a layout pass if the orientation of this
 * LinearLayout is set to {@link #HORIZONTAL}.
 *
 * @see #getOrientation()//  w ww.ja  v a 2 s .c  o m
 * @see #setOrientation(int)
 * @see #onLayout(boolean, int, int, int, int)
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
void layoutHorizontal(int left, int top, int right, int bottom) {
    final boolean isLayoutRtl = ViewUtils.isLayoutRtl(this);
    final int paddingTop = getPaddingTop();

    int childTop;
    int childLeft;

    // Where bottom of child should go
    final int height = bottom - top;
    int childBottom = height - getPaddingBottom();

    // Space available for child
    int childSpace = height - paddingTop - getPaddingBottom();

    final int count = getVirtualChildCount();

    final int majorGravity = mGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;
    final int minorGravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;

    final boolean baselineAligned = mBaselineAligned;

    final int[] maxAscent = mMaxAscent;
    final int[] maxDescent = mMaxDescent;

    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    switch (GravityCompat.getAbsoluteGravity(majorGravity, layoutDirection)) {
    case Gravity.RIGHT:
        // mTotalLength contains the padding already
        childLeft = getPaddingLeft() + right - left - mTotalLength;
        break;

    case Gravity.CENTER_HORIZONTAL:
        // mTotalLength contains the padding already
        childLeft = getPaddingLeft() + (right - left - mTotalLength) / 2;
        break;

    case Gravity.LEFT:
    default:
        childLeft = getPaddingLeft();
        break;
    }

    int start = 0;
    int dir = 1;
    //In case of RTL, start drawing from the last child.
    if (isLayoutRtl) {
        start = count - 1;
        dir = -1;
    }

    for (int i = 0; i < count; i++) {
        int childIndex = start + dir * i;
        final View child = getVirtualChildAt(childIndex);

        if (child == null) {
            childLeft += measureNullChild(childIndex);
        } else if (child.getVisibility() != GONE) {
            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();
            int childBaseline = -1;

            final LinearLayoutCompat.LayoutParams lp = (LinearLayoutCompat.LayoutParams) child
                    .getLayoutParams();

            if (baselineAligned && lp.height != LayoutParams.MATCH_PARENT) {
                childBaseline = child.getBaseline();
            }

            int gravity = lp.gravity;
            if (gravity < 0) {
                gravity = minorGravity;
            }

            switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
            case Gravity.TOP:
                childTop = paddingTop + lp.topMargin;
                if (childBaseline != -1) {
                    childTop += maxAscent[INDEX_TOP] - childBaseline;
                }
                break;

            case Gravity.CENTER_VERTICAL:
                // Removed support for baseline alignment when layout_gravity or
                // gravity == center_vertical. See bug #1038483.
                // Keep the code around if we need to re-enable this feature
                // if (childBaseline != -1) {
                //     // Align baselines vertically only if the child is smaller than us
                //     if (childSpace - childHeight > 0) {
                //         childTop = paddingTop + (childSpace / 2) - childBaseline;
                //     } else {
                //         childTop = paddingTop + (childSpace - childHeight) / 2;
                //     }
                // } else {
                childTop = paddingTop + ((childSpace - childHeight) / 2) + lp.topMargin - lp.bottomMargin;
                break;

            case Gravity.BOTTOM:
                childTop = childBottom - childHeight - lp.bottomMargin;
                if (childBaseline != -1) {
                    int descent = child.getMeasuredHeight() - childBaseline;
                    childTop -= (maxDescent[INDEX_BOTTOM] - descent);
                }
                break;
            default:
                childTop = paddingTop;
                break;
            }

            if (hasDividerBeforeChildAt(childIndex)) {
                childLeft += mDividerWidth;
            }

            childLeft += lp.leftMargin;
            setChildFrame(child, childLeft + getLocationOffset(child), childTop, childWidth, childHeight);
            childLeft += childWidth + lp.rightMargin + getNextLocationOffset(child);

            i += getChildrenSkipCount(child, childIndex);
        }
    }
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

/**
 * Describes how the child views are positioned. Defaults to GRAVITY_TOP. If
 * this layout has a VERTICAL orientation, this controls where all the child
 * views are placed if there is extra vertical space. If this layout has a
 * HORIZONTAL orientation, this controls the alignment of the children.
 *
 * @param gravity See {@link android.view.Gravity}
 *//*ww w.  ja  v a  2  s .c  o m*/
public void setGravity(int gravity) {
    if (mGravity != gravity) {
        if ((gravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
            gravity |= GravityCompat.START;
        }

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

        mGravity = gravity;
        requestLayout();
    }
}

From source file:android.support.v7.widget.LinearLayoutCompat.java

public void setHorizontalGravity(int horizontalGravity) {
    final int gravity = horizontalGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;
    if ((mGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) {
        mGravity = (mGravity & ~GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity;
        requestLayout();/*from   w w w.j a va2  s.  c  om*/
    }
}