Example usage for android.view View getLayoutParams

List of usage examples for android.view View getLayoutParams

Introduction

In this page you can find the example usage for android.view View getLayoutParams.

Prototype

@ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
public ViewGroup.LayoutParams getLayoutParams() 

Source Link

Document

Get the LayoutParams associated with this view.

Usage

From source file:android.percent.support.PercentLayoutHelper.java

/**
 * Iterates over children and restores their original dimensions that were changed for
 * percentage values. Calling this method only makes sense if you previously called
 * {@link PercentLayoutHelper#adjustChildren(int, int)}.
 *//*from w w w .ja  v a 2 s.com*/

public void restoreOriginalParams() {
    for (int i = 0, N = mHost.getChildCount(); i < N; i++) {
        View view = mHost.getChildAt(i);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "should restore " + view + " " + params);
        }
        if (params instanceof PercentLayoutParams) {
            PercentLayoutInfo info = ((PercentLayoutParams) params).getPercentLayoutInfo();
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "using " + info);
            }
            if (info != null) {
                if (params instanceof ViewGroup.MarginLayoutParams) {
                    info.restoreMarginLayoutParams((ViewGroup.MarginLayoutParams) params);
                } else {
                    info.restoreLayoutParams(params);
                }
            }
        }
    }
}

From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int sourceWidth = MeasureSpec.getSize(widthMeasureSpec);
    int widthSize = sourceWidth;
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    // Single-use layout; just ignore the mode and use available space.
    // Clamp to maxWidth.
    if (mMaxWidth >= 0) {
        widthSize = Math.min(widthSize, mMaxWidth);
    }//from ww w .j a  va  2 s .co  m

    final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
    final int heightSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
    final int widthPadding = getPaddingLeft() + getPaddingRight();
    int heightUsed = getPaddingTop() + getPaddingBottom();

    // Measure always-show children first.
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp.alwaysShow && child.getVisibility() != GONE) {
            measureChildWithMargins(child, widthSpec, widthPadding, heightSpec, heightUsed);
            heightUsed += lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
        }
    }

    final int alwaysShowHeight = heightUsed;

    // And now the rest.
    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (!lp.alwaysShow && child.getVisibility() != GONE) {
            measureChildWithMargins(child, widthSpec, widthPadding, heightSpec, heightUsed);
            heightUsed += lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
        }
    }

    mCollapsibleHeight = Math.max(0, heightUsed - alwaysShowHeight - getMaxCollapsedHeight());

    if (mIsLaidOut) {
        mCollapseOffset = Math.min(mCollapseOffset, mCollapsibleHeight);
    } else {
        // Start out collapsed at first unless we restored state for otherwise
        mCollapseOffset = mOpenOnLayout ? 0 : mCollapsibleHeight;
    }

    mTopOffset = Math.max(0, heightSize - heightUsed) + (int) mCollapseOffset;

    setMeasuredDimension(sourceWidth, heightSize);
}

From source file:android.percent.support.PercentLayoutHelper.java

/**
 * Iterates over children and checks if any of them would like to get more space than it
 * received through the percentage dimension.
 * <p/>//  www  .ja  va2 s .c om
 * If you are building a layout that supports percentage dimensions you are encouraged to take
 * advantage of this method. The developer should be able to specify that a child should be
 * remeasured by adding normal dimension attribute with {@code wrap_content} value. For example
 * he might specify child's attributes as {@code app:layout_widthPercent="60%p"} and
 * {@code android:layout_width="wrap_content"}. In this case if the child receives too little
 * space, it will be remeasured with width set to {@code WRAP_CONTENT}.
 *
 * @return True if the measure phase needs to be rerun because one of the children would like
 * to receive more space.
 */
public boolean handleMeasuredStateTooSmall() {
    boolean needsSecondMeasure = false;
    for (int i = 0, N = mHost.getChildCount(); i < N; i++) {
        View view = mHost.getChildAt(i);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "should handle measured state too small " + view + " " + params);
        }
        if (params instanceof PercentLayoutParams) {
            PercentLayoutInfo info = ((PercentLayoutParams) params).getPercentLayoutInfo();
            if (info != null) {
                if (shouldHandleMeasuredWidthTooSmall(view, info)) {
                    needsSecondMeasure = true;
                    params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
                }
                if (shouldHandleMeasuredHeightTooSmall(view, info)) {
                    needsSecondMeasure = true;
                    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                }
            }
        }
    }
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "should trigger second measure pass: " + needsSecondMeasure);
    }
    return needsSecondMeasure;
}

From source file:android.percent.support.PercentLayoutHelper.java

/**
 * Iterates over children and changes their width and height to one calculated from percentage
 * values.//from ww  w . ja v  a 2 s. com
 *
 * @param widthMeasureSpec  Width MeasureSpec of the parent ViewGroup.
 * @param heightMeasureSpec Height MeasureSpec of the parent ViewGroup.
 */
public void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG,
                "adjustChildren: " + mHost + " widthMeasureSpec: " + View.MeasureSpec.toString(widthMeasureSpec)
                        + " heightMeasureSpec: " + View.MeasureSpec.toString(heightMeasureSpec));
    }
    int widthHint = View.MeasureSpec.getSize(widthMeasureSpec);
    int heightHint = View.MeasureSpec.getSize(heightMeasureSpec);

    if (Log.isLoggable(TAG, Log.DEBUG))
        Log.d(TAG, "widthHint = " + widthHint + " , heightHint = " + heightHint);

    for (int i = 0, N = mHost.getChildCount(); i < N; i++) {
        View view = mHost.getChildAt(i);
        ViewGroup.LayoutParams params = view.getLayoutParams();

        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "should adjust " + view + " " + params);
        }

        if (params instanceof PercentLayoutParams) {
            PercentLayoutInfo info = ((PercentLayoutParams) params).getPercentLayoutInfo();
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "using " + info);
            }
            if (info != null) {
                supportTextSize(widthHint, heightHint, view, info);
                supportPadding(widthHint, heightHint, view, info);
                supportMinOrMaxDimesion(widthHint, heightHint, view, info);

                if (params instanceof ViewGroup.MarginLayoutParams) {
                    info.fillMarginLayoutParams((ViewGroup.MarginLayoutParams) params, widthHint, heightHint);
                } else {
                    info.fillLayoutParams(params, widthHint, heightHint);
                }
            }
        }
    }

}

From source file:android.support.v7.internal.widget.ListViewCompat.java

/**
 * Measures the height of the given range of children (inclusive) and returns the height
 * with this ListView's padding and divider heights included. If maxHeight is provided, the
 * measuring will stop when the current height reaches maxHeight.
 *
 * @param widthMeasureSpec             The width measure spec to be given to a child's
 *                                     {@link View#measure(int, int)}.
 * @param startPosition                The position of the first child to be shown.
 * @param endPosition                  The (inclusive) position of the last child to be
 *                                     shown. Specify {@link #NO_POSITION} if the last child
 *                                     should be the last available child from the adapter.
 * @param maxHeight                    The maximum height that will be returned (if all the
 *                                     children don't fit in this value, this value will be
 *                                     returned).
 * @param disallowPartialChildPosition In general, whether the returned height should only
 *                                     contain entire children. This is more powerful--it is
 *                                     the first inclusive position at which partial
 *                                     children will not be allowed. Example: it looks nice
 *                                     to have at least 3 completely visible children, and
 *                                     in portrait this will most likely fit; but in
 *                                     landscape there could be times when even 2 children
 *                                     can not be completely shown, so a value of 2
 *                                     (remember, inclusive) would be good (assuming
 *                                     startPosition is 0).
 * @return The height of this ListView with the given children.
 */// ww  w. ja  v a  2 s  .  c om
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        final ViewGroup.LayoutParams childLp = child.getLayoutParams();
        if (childLp != null && childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

        if (i > 0) {
            // Count the divider for all but one child
            returnedHeight += dividerHeight;
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which height to return.  If returnedHeight >
            // maxHeight, then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                    && (i > disallowPartialChildPosition) // We've past the min pos
                    && (prevHeightWithoutPartialChild > 0) // We have a prev height
                    && (returnedHeight != maxHeight) // i'th child did not fit completely
                            ? prevHeightWithoutPartialChild
                            : maxHeight;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedHeight
    return returnedHeight;
}

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

/**
 * Measures the height of the given range of children (inclusive) and returns the height
 * with this ListView's padding and divider heights included. If maxHeight is provided, the
 * measuring will stop when the current height reaches maxHeight.
 *
 * @param widthMeasureSpec             The width measure spec to be given to a child's
 *                                     {@link View#measure(int, int)}.
 * @param startPosition                The position of the first child to be shown.
 * @param endPosition                  The (inclusive) position of the last child to be
 *                                     shown. Specify {@link #NO_POSITION} if the last child
 *                                     should be the last available child from the adapter.
 * @param maxHeight                    The maximum height that will be returned (if all the
 *                                     children don't fit in this value, this value will be
 *                                     returned).
 * @param disallowPartialChildPosition In general, whether the returned height should only
 *                                     contain entire children. This is more powerful--it is
 *                                     the first inclusive position at which partial
 *                                     children will not be allowed. Example: it looks nice
 *                                     to have at least 3 completely visible children, and
 *                                     in portrait this will most likely fit; but in
 *                                     landscape there could be times when even 2 children
 *                                     can not be completely shown, so a value of 2
 *                                     (remember, inclusive) would be good (assuming
 *                                     startPosition is 0).
 * @return The height of this ListView with the given children.
 *//*from w w  w . j  a v  a 2s. c  o m*/
public int measureHeightOfChildrenCompat(int widthMeasureSpec, int startPosition, int endPosition,
        final int maxHeight, int disallowPartialChildPosition) {

    final int paddingTop = getListPaddingTop();
    final int paddingBottom = getListPaddingBottom();
    final int paddingLeft = getListPaddingLeft();
    final int paddingRight = getListPaddingRight();
    final int reportedDividerHeight = getDividerHeight();
    final Drawable divider = getDivider();

    final ListAdapter adapter = getAdapter();

    if (adapter == null) {
        return paddingTop + paddingBottom;
    }

    // Include the padding of the list
    int returnedHeight = paddingTop + paddingBottom;
    final int dividerHeight = ((reportedDividerHeight > 0) && divider != null) ? reportedDividerHeight : 0;

    // The previous height value that was less than maxHeight and contained
    // no partial children
    int prevHeightWithoutPartialChild = 0;

    View child = null;
    int viewType = 0;
    int count = adapter.getCount();
    for (int i = 0; i < count; i++) {
        int newType = adapter.getItemViewType(i);
        if (newType != viewType) {
            child = null;
            viewType = newType;
        }
        child = adapter.getView(i, child, this);

        // Compute child height spec
        int heightMeasureSpec;
        ViewGroup.LayoutParams childLp = child.getLayoutParams();

        if (childLp == null) {
            childLp = generateDefaultLayoutParams();
            child.setLayoutParams(childLp);
        }

        if (childLp.height > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(childLp.height, MeasureSpec.EXACTLY);
        } else {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }
        child.measure(widthMeasureSpec, heightMeasureSpec);

        // Since this view was measured directly aginst the parent measure
        // spec, we must measure it again before reuse.
        child.forceLayout();

        if (i > 0) {
            // Count the divider for all but one child
            returnedHeight += dividerHeight;
        }

        returnedHeight += child.getMeasuredHeight();

        if (returnedHeight >= maxHeight) {
            // We went over, figure out which height to return.  If returnedHeight >
            // maxHeight, then the i'th position did not fit completely.
            return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
                    && (i > disallowPartialChildPosition) // We've past the min pos
                    && (prevHeightWithoutPartialChild > 0) // We have a prev height
                    && (returnedHeight != maxHeight) // i'th child did not fit completely
                            ? prevHeightWithoutPartialChild
                            : maxHeight;
        }

        if ((disallowPartialChildPosition >= 0) && (i >= disallowPartialChildPosition)) {
            prevHeightWithoutPartialChild = returnedHeight;
        }
    }

    // At this point, we went through the range of children, and they each
    // completely fit, so return the returnedHeight
    return returnedHeight;
}

From source file:android.support.v7.internal.widget.ActionBarOverlayLayout.java

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();

    final int parentLeft = getPaddingLeft();
    final int parentRight = right - left - getPaddingRight();

    final int parentTop = getPaddingTop();
    final int parentBottom = bottom - top - getPaddingBottom();

    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 + lp.leftMargin;
            int childTop;
            if (child == mActionBarBottom) {
                childTop = parentBottom - height - lp.bottomMargin;
            } else {
                childTop = parentTop + lp.topMargin;
            }/*from w  w  w.j av a  2s  . c  o  m*/

            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}

From source file:com.evvsoft.treeview.SimpleJsonTreeViewAdapter.java

private View createViewFromResource(TreeViewNode node, int position, View convertView, ViewGroup parent,
        int resource, Boolean isGroup) {
    View v;//  ww w . j a  va2s. co m
    if (convertView == null || convertView.getId() != (int) node.getId()) {
        v = mInflater.inflate(R.layout.treeview_item_wrapper, parent, false);
        v.setId((int) node.getId());
        View list_item = mInflater.inflate(resource, parent, false);
        ((ViewGroup) v).addView(list_item);

        View indented = v.findViewById(R.id.treeview_item_image);
        int resId = isGroup ? mGroupIndicatorRes : mChildIndicatorRes;
        if (resId != 0)
            ((ImageView) indented).setImageResource(resId);
        else {
            ((ViewGroup) v).removeView(indented);
            indented = list_item;
        }

        ViewGroup.LayoutParams params = indented.getLayoutParams();
        ((LinearLayout.LayoutParams) params).leftMargin = mIndent * node.getLevel();
        indented.requestLayout();
    } else
        v = convertView;

    ImageView image = (ImageView) v.findViewById(R.id.treeview_item_image);
    if (image != null)
        image.setImageState(STATE[node.isExpanded() ? STATE_EXPANDED
                : !node.isGroupNode() && node.isLast() ? STATE_LAST : STATE_NONE], true);

    bindView(node, v);

    return v;
}

From source file:android.support.v7.internal.widget.ActionBarOverlayLayout.java

private boolean applyInsets(View view, Rect insets, boolean left, boolean top, boolean bottom, boolean right) {
    boolean changed = false;
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    if (left && lp.leftMargin != insets.left) {
        changed = true;/*w w  w. ja v  a2 s  . c  o m*/
        lp.leftMargin = insets.left;
    }
    if (top && lp.topMargin != insets.top) {
        changed = true;
        lp.topMargin = insets.top;
    }
    if (right && lp.rightMargin != insets.right) {
        changed = true;
        lp.rightMargin = insets.right;
    }
    if (bottom && lp.bottomMargin != insets.bottom) {
        changed = true;
        lp.bottomMargin = insets.bottom;
    }
    return changed;
}

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

private void assignSpans(RecyclerView.Recycler recycler, RecyclerView.State state, int count,
        int consumedSpanCount, boolean layingOutInPrimaryDirection) {
    int span, spanDiff, start, end, diff;
    // make sure we traverse from min position to max position
    if (layingOutInPrimaryDirection) {
        start = 0;/*from w  ww.  j  a  va 2  s  .  c  om*/
        end = count;
        diff = 1;
    } else {
        start = count - 1;
        end = -1;
        diff = -1;
    }
    if (mOrientation == VERTICAL && isLayoutRTL()) { // start from last span
        span = mSpanCount - 1;
        spanDiff = -1;
    } else {
        span = 0;
        spanDiff = 1;
    }
    for (int i = start; i != end; i += diff) {
        View view = mSet[i];
        LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.mSpanSize = getSpanSize(recycler, state, getPosition(view));
        if (spanDiff == -1 && params.mSpanSize > 1) {
            params.mSpanIndex = span - (params.mSpanSize - 1);
        } else {
            params.mSpanIndex = span;
        }
        span += spanDiff * params.mSpanSize;
    }
}