Java Code Examples for android.view.View.getLayoutParams()

The following are Jave code examples for showing how to use getLayoutParams() of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: PageScrollView   File: BaseViewGroup.java   View Source Code Vote up 6 votes
protected int offsetY(View child, boolean centreInVisibleBounds, boolean marginInclude) {
    int current;
    MarginLayoutParams marginLp = (marginInclude && child.getLayoutParams() instanceof MarginLayoutParams) ? (MarginLayoutParams) child.getLayoutParams() : null;
    if (centreInVisibleBounds) {
        current = (child.getTop() + child.getBottom()) >> 1;
        if (marginLp != null) {
            current = current + (marginLp.bottomMargin - marginLp.topMargin) / 2;
        }
        return current - mVisibleBounds.centerY() + mVisibleBounds.top - getPaddingTop();
    } else {
        current = child.getTop();
        if (marginLp != null) {
            current = current - marginLp.topMargin;
        }
        return current - getPaddingTop();
    }
}
 
Example 2
Project: FilterBar   File: UIUtil.java   View Source Code Vote up 6 votes
public static void measureView(View child) {
    ViewGroup.LayoutParams p = child.getLayoutParams();
    if (p == null) {
        p = new ViewGroup.LayoutParams(-1, -2);
    }

    int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = View.MeasureSpec.makeMeasureSpec(lpHeight, 1073741824);
    } else {
        childHeightSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
    }

    child.measure(childWidthSpec, childHeightSpec);
}
 
Example 3
Project: Swap   File: PLA_ListView.java   View Source Code Vote up 6 votes
private void measureScrapChild(View child, int position, int widthMeasureSpec) {
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, 0);
        child.setLayoutParams(p);
    }
    p.viewType = mAdapter.getItemViewType(position);
    p.forceAdd = true;

    int childWidthSpec = ViewGroup.getChildMeasureSpec(widthMeasureSpec,
            mListPadding.left + mListPadding.right, p.width);
    int lpHeight = p.height;
    int childHeightSpec;
    if (lpHeight > 0) {
        childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
    } else {
        childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    }
    child.measure(childWidthSpec, childHeightSpec);
}
 
Example 4
Project: airgram   File: LinearLayoutManager.java   View Source Code Vote up 6 votes
/**
 * Returns the next item from the scrap list.
 * <p>
 * Upon finding a valid VH, sets current item position to VH.itemPosition + mItemDirection
 *
 * @return View if an item in the current position or direction exists if not null.
 */
private View nextViewFromScrapList() {
    final int size = mScrapList.size();
    for (int i = 0; i < size; i++) {
        final View view = mScrapList.get(i).itemView;
        final LayoutParams lp = (LayoutParams) view.getLayoutParams();
        if (lp.isItemRemoved()) {
            continue;
        }
        if (mCurrentPosition == lp.getViewLayoutPosition()) {
            assignPositionFromScrapList(view);
            return view;
        }
    }
    return null;
}
 
Example 5
Project: RecyclerViewEvent   File: DividerListItemDecoration.java   View Source Code Vote up 6 votes
public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 6
Project: boohee_v5.6   File: ActionBarOverlayLayout.java   View Source Code Vote up 6 votes
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    int count = getChildCount();
    int parentLeft = getPaddingLeft();
    int parentRight = (right - left) - getPaddingRight();
    int parentTop = getPaddingTop();
    int parentBottom = (bottom - top) - getPaddingBottom();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() != 8) {
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childLeft = parentLeft + lp.leftMargin;
            int childTop = parentTop + lp.topMargin;
            child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight());
        }
    }
}
 
Example 7
Project: TPlayer   File: CenterLayout.java   View Source Code Vote up 6 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int count = getChildCount();
  mWidth = getMeasuredWidth();
  mHeight = getMeasuredHeight();
  for (int i = 0; i < count; i++) {
    View child = getChildAt(i);
    if (child.getVisibility() != GONE) {
      LayoutParams lp = (LayoutParams) child.getLayoutParams();
      int childLeft = mPaddingLeft + lp.x;
      if (lp.width > 0)
        childLeft += (int) ((mWidth - lp.width) / 2.0);
      else
        childLeft += (int) ((mWidth - child.getMeasuredWidth()) / 2.0);
      int childTop = mPaddingTop + lp.y;
      if (lp.height > 0)
        childTop += (int) ((mHeight - lp.height) / 2.0);
      else
        childTop += (int) ((mHeight - child.getMeasuredHeight()) / 2.0);
      child.layout(childLeft, childTop, childLeft + child.getMeasuredWidth(), childTop + child.getMeasuredHeight());
    }
  }
}
 
Example 8
Project: GitHub   File: Utils.java   View Source Code Vote up 5 votes
static int getMarginStart(View v) {
  if (v == null) {
    return 0;
  }
  ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
  return MarginLayoutParamsCompat.getMarginStart(lp);
}
 
Example 9
Project: TextReader   File: SupportGridItemDecoration.java   View Source Code Vote up 5 votes
public void drawHorizontal(Canvas c, RecyclerView parent) {
    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getLeft() - params.leftMargin;
        final int right = child.getRight() + params.rightMargin
                + mDivider.getIntrinsicWidth();
        final int top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 10
Project: filepicker   File: DividerListItemDecoration.java   View Source Code Vote up 5 votes
private void drawVerticalDivider(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}
 
Example 11
Project: Botanist   File: PlantActivity.java   View Source Code Vote up 5 votes
/**
 * Expand a hidden view
 * @param v - current view
 */
private static void expand(final View v) {
    v.measure(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();
    Animation anim = new Animation() {
        /**
         * Transform a view
         * @param interpolatedTime - time to transition
         * @param t - transition to apply
         */
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? CoordinatorLayout.LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        /**
         * Returns if the bounds will change
         * @return Returns true
         */
        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };
    // 3dp/ms
    anim.setDuration(((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density)) * 3);
    v.startAnimation(anim);
}
 
Example 12
Project: UIWidget   File: UIActionSheetView.java   View Source Code Vote up 5 votes
public ViewGroup.MarginLayoutParams setViewMargin(View view, int left, int top, int right, int bottom) {
    if (view == null) {
        return null;
    }
    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
    if (lp != null) {
        lp.setMargins(left, top, right, bottom);
        view.setLayoutParams(lp);
    }
    return lp;
}
 
Example 13
Project: boohee_v5.6   File: DrawerLayout.java   View Source Code Vote up 5 votes
public boolean isDrawerOpen(View drawer) {
    if (!isDrawerView(drawer)) {
        throw new IllegalArgumentException("View " + drawer + " is not a drawer");
    } else if ((((LayoutParams) drawer.getLayoutParams()).openState & 1) == 1) {
        return true;
    } else {
        return false;
    }
}
 
Example 14
Project: GitHub   File: GridItemDecoration.java   View Source Code Vote up 5 votes
/**
 * @param c
 * @param parent
 * @param state
 */
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    if (dividerDrawable == null) {
        return;
    }

    int childCount = parent.getChildCount();
    int rightV = parent.getWidth();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int leftV = parent.getPaddingLeft() + child.getPaddingLeft();
        int bottomV = child.getTop() - params.topMargin;
        int topV = bottomV - dividerDrawable.getIntrinsicHeight();

        int topH = child.getTop() + params.topMargin;
        int bottomH = child.getBottom() + params.bottomMargin;
        int rightH = child.getLeft() - params.leftMargin;
        int leftH = rightH - dividerDrawable.getIntrinsicWidth();
        dividerDrawable.setBounds(leftH, topH, rightH, bottomH);
        dividerDrawable.draw(c);
        dividerDrawable.setBounds(leftV, topV, rightV, bottomV);
        dividerDrawable.draw(c);
    }
}
 
Example 15
Project: BBSSDK-for-Android   File: StatusBarCompatKitKat.java   View Source Code Vote up 5 votes
/**
 * add marginTop to simulate set FitsSystemWindow true
 */
private static void addMarginTopToContentChild(View mContentChild, int statusBarHeight) {
	if (mContentChild == null) {
		return;
	}
	if (!TAG_MARGIN_ADDED.equals(mContentChild.getTag())) {
		FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentChild.getLayoutParams();
		lp.topMargin += statusBarHeight;
		mContentChild.setLayoutParams(lp);
		mContentChild.setTag(TAG_MARGIN_ADDED);
	}
}
 
Example 16
Project: Hitalk   File: HeaderPositionCalculator.java   View Source Code Vote up 5 votes
private void initDefaultHeaderOffset(Rect headerMargins, RecyclerView recyclerView, View header, View firstView, int orientation) {
  int translationX, translationY;
  mDimensionCalculator.initMargins(mTempRect1, header);

  ViewGroup.LayoutParams layoutParams = firstView.getLayoutParams();
  int leftMargin = 0;
  int topMargin = 0;
  if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
    ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) layoutParams;
    leftMargin = marginLayoutParams.leftMargin;
    topMargin = marginLayoutParams.topMargin;
  }

  if (orientation == LinearLayoutManager.VERTICAL) {
    translationX = firstView.getLeft() - leftMargin + mTempRect1.left;
    translationY = Math.max(
        firstView.getTop() - topMargin - header.getHeight() - mTempRect1.bottom,
        getListTop(recyclerView) + mTempRect1.top);
  } else {
    translationY = firstView.getTop() - topMargin + mTempRect1.top;
    translationX = Math.max(
        firstView.getLeft() - leftMargin - header.getWidth() - mTempRect1.right,
        getListLeft(recyclerView) + mTempRect1.left);
  }

  headerMargins.set(translationX, translationY, translationX + header.getWidth(),
          translationY + header.getHeight());
}
 
Example 17
Project: airgram   File: LinearLayoutManager.java   View Source Code Vote up 4 votes
void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state,
        LayoutState layoutState, LayoutChunkResult result) {
    View view = layoutState.next(recycler);
    if (view == null) {
        if (DEBUG && layoutState.mScrapList == null) {
            throw new RuntimeException("received null view when unexpected");
        }
        // if we are laying out views in scrap, this may return null which means there is
        // no more items to layout.
        result.mFinished = true;
        return;
    }
    LayoutParams params = (LayoutParams) view.getLayoutParams();
    if (layoutState.mScrapList == null) {
        if (mShouldReverseLayout == (layoutState.mLayoutDirection
                == LayoutState.LAYOUT_START)) {
            addView(view);
        } else {
            addView(view, 0);
        }
    } else {
        if (mShouldReverseLayout == (layoutState.mLayoutDirection
                == LayoutState.LAYOUT_START)) {
            addDisappearingView(view);
        } else {
            addDisappearingView(view, 0);
        }
    }
    measureChildWithMargins(view, 0, 0);
    result.mConsumed = mOrientationHelper.getDecoratedMeasurement(view);
    int left, top, right, bottom;
    if (mOrientation == VERTICAL) {
        if (isLayoutRTL()) {
            right = getWidth() - getPaddingRight();
            left = right - mOrientationHelper.getDecoratedMeasurementInOther(view);
        } else {
            left = getPaddingLeft();
            right = left + mOrientationHelper.getDecoratedMeasurementInOther(view);
        }
        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            bottom = layoutState.mOffset;
            top = layoutState.mOffset - result.mConsumed;
        } else {
            top = layoutState.mOffset;
            bottom = layoutState.mOffset + result.mConsumed;
        }
    } else {
        top = getPaddingTop();
        bottom = top + mOrientationHelper.getDecoratedMeasurementInOther(view);

        if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) {
            right = layoutState.mOffset;
            left = layoutState.mOffset - result.mConsumed;
        } else {
            left = layoutState.mOffset;
            right = layoutState.mOffset + result.mConsumed;
        }
    }
    // We calculate everything with View's bounding box (which includes decor and margins)
    // To calculate correct layout position, we subtract margins.
    layoutDecorated(view, left + params.leftMargin, top + params.topMargin,
            right - params.rightMargin, bottom - params.bottomMargin);
    if (DEBUG) {
        Log.d(TAG, "laid out child at position " + getPosition(view) + ", with l:"
                + (left + params.leftMargin) + ", t:" + (top + params.topMargin) + ", r:"
                + (right - params.rightMargin) + ", b:" + (bottom - params.bottomMargin));
    }
    // Consume the available space if the view is not removed OR changed
    if (params.isItemRemoved() || params.isItemChanged()) {
        result.mIgnoreConsumed = true;
    }
    result.mFocusable = view.isFocusable();
}
 
Example 18
Project: vlayout   File: RangeStyle.java   View Source Code Vote up 4 votes
public void adjustLayout(int startPosition, int endPosition, LayoutManagerHelper helper) {
    if (!isChildrenEmpty()) {
        for (int i = 0, size = mChildren.size(); i < size; i++) {
            RangeStyle rangeStyle = mChildren.valueAt(i);
            rangeStyle.adjustLayout(startPosition, endPosition, helper);
        }
    }
    if (requireLayoutView()) {
        View refer = null;
        Rect tempRect = new Rect();
        final OrientationHelperEx orientationHelper = helper.getMainOrientationHelper();
        for (int i = 0; i < helper.getChildCount(); i++) {
            refer = helper.getChildAt(i);
            int anchorPos = helper.getPosition(refer);
            if (getRange().contains(anchorPos)) {
                if (refer.getVisibility() == View.GONE) {
                    tempRect.setEmpty();
                } else {
                    final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)
                        refer.getLayoutParams();
                    if (helper.getOrientation() == VirtualLayoutManager.VERTICAL) {
                        tempRect.union(helper.getDecoratedLeft(refer) - params.leftMargin,
                            orientationHelper.getDecoratedStart(refer),
                            helper.getDecoratedRight(refer) + params.rightMargin,
                            orientationHelper.getDecoratedEnd(refer));
                    } else {
                        tempRect.union(orientationHelper.getDecoratedStart(refer),
                            helper.getDecoratedTop(refer) - params.topMargin, orientationHelper.getDecoratedEnd(refer),
                            helper.getDecoratedBottom(refer) + params.bottomMargin);
                    }
                }
            }
        }
        if (!tempRect.isEmpty()) {
            mLayoutRegion.set(tempRect.left - mPaddingLeft, tempRect.top - mPaddingTop,
                tempRect.right + mPaddingRight, tempRect.bottom + mPaddingBottom);
        } else {
            mLayoutRegion.setEmpty();
        }
        if (mLayoutView != null) {
            mLayoutView.layout(mLayoutRegion.left, mLayoutRegion.top, mLayoutRegion.right, mLayoutRegion.bottom);
        }
    }
}
 
Example 19
Project: SimpleUILauncher   File: CellLayout.java   View Source Code Vote up 4 votes
public void markCellsAsUnoccupiedForView(View view) {
    if (view == null || view.getParent() != mShortcutsAndWidgets) return;
    LayoutParams lp = (LayoutParams) view.getLayoutParams();
    mOccupied.markCells(lp.cellX, lp.cellY, lp.cellHSpan, lp.cellVSpan, false);
}
 
Example 20
Project: baselibrary-master   File: BaseFragment.java   View Source Code Vote up 3 votes
/**
 * 设置控件宽度高度
 *
 * @param v      控件
 * @param width  宽度,MATCH_PARENT = -1, WRAP_CONTENT = -2;
 * @param height 高度,MATCH_PARENT = -1, WRAP_CONTENT = -2;
 */
public void setHeight(@NonNull View v, int width, int height) {
    ViewGroup.LayoutParams lp = v.getLayoutParams();
    lp.height = height;
    lp.width = width;
    v.setLayoutParams(lp);
}