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

The following are Jave code examples for showing how to use layout() 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.
Example 1
Project: SimpleUILauncher   File: ShortcutAndWidgetContainer.java   Source Code and License Vote up 7 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
            int childLeft = lp.x;
            int childTop = lp.y;
            child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);//对子对象进行布局。

            if (lp.dropped) {
                lp.dropped = false;

                final int[] cellXY = mTmpCellXY;
                getLocationOnScreen(cellXY);
                mWallpaperManager.sendWallpaperCommand(getWindowToken(),
                        WallpaperManager.COMMAND_DROP,
                        cellXY[0] + childLeft + lp.width / 2,
                        cellXY[1] + childTop + lp.height / 2, 0, null);
            }
        }
    }
}
 
Example 2
Project: Blockly   File: InputView.java   Source Code and License Vote up 7 votes
@Override
public void onLayout(boolean changed, int l, int t, int r, int b) {
    boolean rtl = mHelper.useRtl();
    int viewWidth = getMeasuredWidth();

    // Initialize horizontal layout cursor. The cursor value increases as layout proceeds, but
    // actual layout locations in RTL mode are (viewWidth - cursorX).
    int cursorX = getFirstFieldX();

    for (int i = 0; i < mFieldViews.size(); i++) {
        View view = (View) mFieldViews.get(i);

        // Use the width and height of the field's view to compute its placement
        int fieldWidth = view.getMeasuredWidth();
        int fieldHeight = view.getMeasuredHeight();

        int left = rtl ? viewWidth - (cursorX + fieldWidth) : cursorX;
        view.layout(left, mBlockTopPadding, left + fieldWidth,
                mBlockTopPadding + fieldHeight);

        // Move x position left or right, depending on RTL mode.
        cursorX += fieldWidth + mHorizontalFieldSpacing;
    }

    layoutChild();
}
 
Example 3
Project: QuanMinTV   File: CenterLayout.java   Source Code and License Vote up 7 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) {
      CenterLayout.LayoutParams lp = (CenterLayout.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 4
Project: AndroidRTC   File: PercentFrameLayout.java   Source Code and License Vote up 7 votes
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int width = right - left;
    final int height = bottom - top;
    // Sub-rectangle specified by percentage values.
    final int subWidth = width * widthPercent / 100;
    final int subHeight = height * heightPercent / 100;
    final int subLeft = left + width * xPercent / 100;
    final int subTop = top + height * yPercent / 100;

    for (int i = 0; i < getChildCount(); ++i) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final int childWidth = child.getMeasuredWidth();
            final int childHeight = child.getMeasuredHeight();
            // Center child both vertically and horizontally.
            final int childLeft = subLeft + (subWidth - childWidth) / 2;
            final int childTop = subTop + (subHeight - childHeight) / 2;
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
    }
}
 
Example 5
Project: GitHub   File: AnimRFRecyclerView.java   Source Code and License Vote up 7 votes
private void updateViewSize(Message msg) {
    // 重新设置View的宽高
    if (msg.obj != null) {
        rfView.headerImage.getLayoutParams().height += msg.arg1;
        View view = ((View) msg.obj);
        view.layout(view.getLeft(), 0, view.getRight(), view.getBottom());
    } else {
        // 实现类似弹簧的阻力效果,拉的越长就越难拉的动
        rfView.headerImageScaleHeight = rfView.headerImage.getLayoutParams().height
                - rfView.headerImageHeight;
        if (rfView.headerImageScaleHeight < (rfView.headerImageMaxHeight - rfView.headerImageHeight) / 3) {
            rfView.headerImage.getLayoutParams().height -= msg.arg1;
        } else if (rfView.headerImageScaleHeight > (rfView.headerImageMaxHeight - rfView.headerImageHeight) / 3 * 2) {
            rfView.headerImage.getLayoutParams().height -= msg.arg1 / 3 * 2;
        } else {
            rfView.headerImage.getLayoutParams().height -= msg.arg1 / 3 * 1.5;
        }
    }
    rfView.headerImage.requestLayout();
}
 
Example 6
Project: Circular-Layout   File: CircularLayout.java   Source Code and License Vote up 6 votes
private void layoutChild(View child) {
    int childWidth = child.getMeasuredWidth();
    int childHeight = child.getMeasuredHeight();
    childRect.top = childCenter.y - childHeight / 2;
    childRect.left = childCenter.x - childWidth / 2;
    childRect.right = childRect.left + childWidth;
    childRect.bottom = childRect.top + childHeight;
    child.layout(childRect.left, childRect.top, childRect.right, childRect.bottom);
}
 
Example 7
Project: boohee_v5.6   File: CoordinatorLayout.java   Source Code and License Vote up 6 votes
private void layoutChildWithAnchor(View child, View anchor, int layoutDirection) {
    LayoutParams lp = (LayoutParams) child.getLayoutParams();
    Rect anchorRect = this.mTempRect1;
    Rect childRect = this.mTempRect2;
    getDescendantRect(anchor, anchorRect);
    getDesiredAnchoredChildRect(child, layoutDirection, anchorRect, childRect);
    child.layout(childRect.left, childRect.top, childRect.right, childRect.bottom);
}
 
Example 8
Project: aos-Video   File: MovieCover.java   Source Code and License Vote up 6 votes
@Override
public Bitmap getDescription( ArtworkFactory factory ) {
	View view;

	// Inflate the layout in case it has not been inflated yet for this Cover class
	if (sDescriptionViewMovie==null) {
		inflateDescriptionLayoutMovie(factory);
	}
	view = sDescriptionViewMovie;

	if (checkScraperInfo(factory)) {
		sMovieTitle.setText(mMovieInfo.getTitle());

		if ((mMovieInfo.getDirectors()!=null) && mMovieInfo.getDirectors().length()>0) {
			sMovieDirector.setText(mMovieInfo.getDirectors());
		}
		else { //fall-back on year if there is no director info
			sMovieDirector.setText(Integer.toString(mMovieInfo.getYear()));
		}
	}
	else {
		// Scraper info not available or not valid => fall-back on filename (this is not a common expected use-case...)
		sMovieTitle.setText(factory.removeFilenameExtension((new File(mFilepath)).getName()));
		sMovieDirector.setText("-");
	}

	if (mDurationMs!=0) {
		sMovieDuration.setText(factory.formatTime(mDurationMs));
	} else {
		sMovieDuration.setText("");
	}

	// Update the layout setup to take care of the updated text views
	view.measure(View.MeasureSpec.makeMeasureSpec(DESCRIPTION_TEXTURE_WIDTH, View.MeasureSpec.EXACTLY),
				 View.MeasureSpec.makeMeasureSpec(DESCRIPTION_TEXTURE_HEIGHT, View.MeasureSpec.AT_MOST));
	view.layout(0, 0, DESCRIPTION_TEXTURE_WIDTH, DESCRIPTION_TEXTURE_HEIGHT);

	return factory.createViewBitmap(view, DESCRIPTION_TEXTURE_WIDTH, DESCRIPTION_TEXTURE_HEIGHT);
}
 
Example 9
Project: UiLib   File: SimpleViewSwitcher.java   Source Code and License Vote up 6 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            child.layout(0, 0,  r - l,  b - t);

        }
    }
}
 
Example 10
Project: Building-Android-UIs-with-Custom-Views   File: CustomLayout.java   Source Code and License Vote up 6 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int count = getChildCount();
    int left = l + getPaddingLeft();
    int top = t + getPaddingTop();

    // keeps track of maximum row height
    int rowHeight = 0;

    for(int i = 0; i < count; i++) {
        View child = getChildAt(i);

        int childWidth = child.getMeasuredWidth();
        int childHeight = child.getMeasuredHeight();

        // if child fits in this row put it there
        if(left + childWidth < r - getPaddingRight()) {
            child.layout(left, top, left + childWidth, top + childHeight);
            left += childWidth;
        } else {
            // otherwise put it on next row
            left = l + getPaddingLeft();
            top += rowHeight;
            rowHeight = 0;
        }

        // update maximum row height
        if(childHeight > rowHeight) rowHeight = childHeight;
    }
}
 
Example 11
Project: boohee_v5.6   File: SimpleItemAnimator.java   Source Code and License Vote up 6 votes
public boolean animateDisappearance(@NonNull ViewHolder viewHolder, @NonNull ItemHolderInfo preLayoutInfo, @Nullable ItemHolderInfo postLayoutInfo) {
    int oldLeft = preLayoutInfo.left;
    int oldTop = preLayoutInfo.top;
    View disappearingItemView = viewHolder.itemView;
    int newLeft = postLayoutInfo == null ? disappearingItemView.getLeft() : postLayoutInfo.left;
    int newTop = postLayoutInfo == null ? disappearingItemView.getTop() : postLayoutInfo.top;
    if (viewHolder.isRemoved() || (oldLeft == newLeft && oldTop == newTop)) {
        return animateRemove(viewHolder);
    }
    disappearingItemView.layout(newLeft, newTop, disappearingItemView.getWidth() + newLeft, disappearingItemView.getHeight() + newTop);
    return animateMove(viewHolder, oldLeft, oldTop, newLeft, newTop);
}
 
Example 12
Project: CustomWeight   File: SlideMenu.java   Source Code and License Vote up 6 votes
/**
 * 摆放子 View
 *
 * @param changed 当前控件的尺寸大小, 位置 是否发生了变化
 * @param l       左边
 * @param t       顶部
 * @param r       右边
 * @param b       底部
 */
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
	View leftMenu = getChildAt(0);
	/*摆放 侧面板位置*/
	leftMenu.layout(-leftMenu.getMeasuredWidth(), 0, 0, b);

	/*摆放主面板*/
	getChildAt(1).layout(l, t, r, b);
}
 
Example 13
Project: LaunchEnr   File: ShortcutAndWidgetContainer.java   Source Code and License Vote up 6 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();

            if (child instanceof LauncherAppWidgetHostView) {
                LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;

                // Scale and center the widget to fit within its cells.
                DeviceProfile profile = mLauncher.getDeviceProfile();
                float scaleX = profile.appWidgetScale.x;
                float scaleY = profile.appWidgetScale.y;

                lahv.setScaleToFit(Math.min(scaleX, scaleY));
                lahv.setTranslationForCentering(-(lp.width - (lp.width * scaleX)) / 2.0f,
                        -(lp.height - (lp.height * scaleY)) / 2.0f);
            }

            int childLeft = lp.x;
            int childTop = lp.y;
            child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height);

            if (lp.dropped) {
                lp.dropped = false;

                final int[] cellXY = mTmpCellXY;
                getLocationOnScreen(cellXY);
                mWallpaperManager.sendWallpaperCommand(getWindowToken(),
                        WallpaperManager.COMMAND_DROP,
                        cellXY[0] + childLeft + lp.width / 2,
                        cellXY[1] + childTop + lp.height / 2, 0, null);
            }
        }
    }
}
 
Example 14
Project: airgram   File: GroupCreateActivity.java   Source Code and License Vote up 6 votes
private ChipSpan createAndPutChipForUser(TLRPC.User user) {
    LayoutInflater lf = (LayoutInflater) ApplicationLoader.applicationContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View textView = lf.inflate(R.layout.group_create_bubble, null);
    TextView text = (TextView)textView.findViewById(R.id.bubble_text_view);
    String name = UserObject.getUserName(user);
    if (name.length() == 0 && user.phone != null && user.phone.length() != 0) {
        name = PhoneFormat.getInstance().format("+" + user.phone);
    }
    text.setText(name + ", ");

    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(spec, spec);
    textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);
    canvas.translate(-textView.getScrollX(), -textView.getScrollY());
    textView.draw(canvas);
    textView.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = textView.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    textView.destroyDrawingCache();

    final BitmapDrawable bmpDrawable = new BitmapDrawable(b);
    bmpDrawable.setBounds(0, 0, b.getWidth(), b.getHeight());

    SpannableStringBuilder ssb = new SpannableStringBuilder("");
    ChipSpan span = new ChipSpan(bmpDrawable, ImageSpan.ALIGN_BASELINE);
    allSpans.add(span);
    selectedContacts.put(user.id, span);
    for (ImageSpan sp : allSpans) {
        ssb.append("<<");
        ssb.setSpan(sp, ssb.length() - 2, ssb.length(), SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    userSelectEditText.setText(ssb);
    userSelectEditText.setSelection(ssb.length());
    return span;
}
 
Example 15
Project: RNLearn_Project1   File: FlatViewGroup.java   Source Code and License Vote up 6 votes
private void processLayoutRequest() {
  mIsLayoutRequested = false;
  for (int i = 0, childCount = getChildCount(); i != childCount; ++i) {
    View child = getChildAt(i);
    if (!child.isLayoutRequested()) {
      continue;
    }

    child.measure(
      MeasureSpec.makeMeasureSpec(child.getWidth(), MeasureSpec.EXACTLY),
      MeasureSpec.makeMeasureSpec(child.getHeight(), MeasureSpec.EXACTLY));
    child.layout(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
  }
}
 
Example 16
Project: BalaFM   File: FlowLayout.java   Source Code and License Vote up 6 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if (changed) {
        //摆放子view
        View child = null;
        //初始子view摆放的左上位置
        int left = getPaddingLeft();
        int top = getPaddingTop();
        //一行view中将最大的高度存于此变量,用于子view进行换行时高度的计算
        int maxHeightInLine = 0;
        for (int i = 0, len = getChildCount(); i < len; i++) {
            child = getChildAt(i);
            //从第二个子view开始算起
            //因为第一个子view默认从头开始摆放
            if (i > 0) {
                //两两对比,取得一行中最大的高度
                if (getChildAt(i - 1).getMeasuredHeight() > maxHeightInLine) {
                    maxHeightInLine = getChildAt(i - 1).getMeasuredHeight();
                }
                //当前子view的起始left为 上一个子view的宽度+水平间距
                left += getChildAt(i - 1).getMeasuredWidth() + dip2px(horizontalSpace);
                if (left + child.getMeasuredWidth() >= getWidth() - getPaddingRight() - getPaddingLeft()) {//这一行所有子view相加的宽度大于容器的宽度,需要换行
                    //换行的首个子view,起始left应该为0+容器的paddingLeft
                    left = getPaddingLeft();
                    //top的位置为上一行中拥有最大高度的某个View的高度+垂直间距
                    top += maxHeightInLine + dip2px(verticalSpace);
                    //将上一行View的最大高度置0
                    maxHeightInLine = 0;
                }
            }
            //摆放子view
            child.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());
        }
    }
}
 
Example 17
Project: SmoothRefreshLayout   File: SmoothRefreshLayout.java   Source Code and License Vote up 5 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    if (count == 0)
        return;
    checkViewsZAxisNeedReset();
    final int paddingLeft = getPaddingLeft();
    final int paddingTop = getPaddingTop();
    final int parentRight = r - l - getPaddingRight();
    final int parentBottom = b - t - getPaddingBottom();
    final boolean isMovingHeader = isMovingHeader();
    final boolean isMovingFooter = isMovingFooter();
    int offsetHeaderY = 0;
    int offsetFooterY = 0;
    if (isMovingHeader) {
        offsetHeaderY = mIndicator.getCurrentPos();
    } else if (isMovingFooter) {
        offsetFooterY = mIndicator.getCurrentPos();
    }
    int contentBottom = 0;
    boolean pin = (mScrollTargetView != null && !isMovingHeader) || isEnabledPinContentView();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE)
            continue;
        if (mHeaderView != null && child == mHeaderView.getView()) {
            layoutHeaderView(child, offsetHeaderY);
        } else if (mTargetView != null && child == mTargetView
                || (mPreviousState != STATE_NONE && mChangeStateAnimator != null
                && mChangeStateAnimator.isRunning() && getView(mPreviousState) == child)) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            final int left = paddingLeft + lp.leftMargin;
            final int right = left + child.getMeasuredWidth();
            int top, bottom;
            if (isMovingHeader) {
                top = paddingTop + lp.topMargin + (pin ? 0 : offsetHeaderY);
                bottom = top + child.getMeasuredHeight();
                child.layout(left, top, right, bottom);
            } else if (isMovingFooter) {
                top = paddingTop + lp.topMargin - (pin ? 0 : offsetFooterY);
                bottom = top + child.getMeasuredHeight();
                child.layout(left, top, right, bottom);
            } else {
                top = paddingTop + lp.topMargin;
                bottom = top + child.getMeasuredHeight();
                child.layout(left, top, right, bottom);
            }
            if (sDebug) {
                SRLog.d(TAG, "onLayout(): content: %s %s %s %s", left, top, right, bottom);
            }
            contentBottom = bottom + lp.bottomMargin;
        } else if (mFooterView == null || mFooterView.getView() != child) {
            layoutOtherViewUseGravity(child, parentRight, parentBottom);
        }
    }
    if (mFooterView != null && mFooterView.getView().getVisibility() != GONE) {
        layoutFooterView(mFooterView.getView(), offsetFooterY, pin, contentBottom);
    }
    tryToPerformAutoRefresh();
}
 
Example 18
Project: boohee_v5.6   File: LinearLayoutCompat.java   Source Code and License Vote up 5 votes
private void setChildFrame(View child, int left, int top, int width, int height) {
    child.layout(left, top, left + width, top + height);
}
 
Example 19
Project: gridlayout   File: FGridLayout.java   Source Code and License Vote up 5 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
    int row = 0;
    int col = 0;
    int left = 0;
    int top = 0;

    final int count = getChildCount();
    for (int i = 0; i < count; i++)
    {
        row = getRowIndex(i);
        col = getColumnIndex(i);

        final View child = getChildAt(i);
        if (row == 0)
        {
            top = getPaddingTop();
        }
        if (col == 0)
        {
            left = getPaddingLeft();
        }

        int right = left + child.getMeasuredWidth();
        int bottom = top + child.getMeasuredHeight();

        child.layout(left, top, right, bottom);

        if (mOrientation == VERTICAL)
        {
            //下一列的left
            left = right + getVerticalSpacing();
            if (col + 1 == mSpanCount)
            {
                //下一行的top
                top += mArrRowHeight.get(row) + getHorizontalSpacing();
            }
        } else
        {
            //下一行的top
            top += mArrRowHeight.get(row) + getHorizontalSpacing();
            if (row + 1 == mSpanCount)
            {
                //下一列的left
                left = right + getVerticalSpacing();
            }
        }
    }
}
 
Example 20
Project: buildAPKsSamples   File: FitCenterFrameLayout.java   Source Code and License Vote up 5 votes
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int childCount = getChildCount();

    final int parentLeft = getPaddingLeft();
    final int parentTop = getPaddingTop();
    final int parentRight = r - l - getPaddingRight();
    final int parentBottom = b - t - getPaddingBottom();

    final int parentWidth = parentRight - parentLeft;
    final int parentHeight = parentBottom - parentTop;

    int unpaddedWidth, unpaddedHeight, parentUnpaddedWidth, parentUnpaddedHeight;
    int childPaddingLeft, childPaddingTop, childPaddingRight, childPaddingBottom;

    for (int i = 0; i < childCount; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() == GONE) {
            continue;
        }

        // Fit and center the child within the parent. Make sure not to consider padding
        // as part of the child's aspect ratio.

        childPaddingLeft = child.getPaddingLeft();
        childPaddingTop = child.getPaddingTop();
        childPaddingRight = child.getPaddingRight();
        childPaddingBottom = child.getPaddingBottom();

        unpaddedWidth = child.getMeasuredWidth() - childPaddingLeft - childPaddingRight;
        unpaddedHeight = child.getMeasuredHeight() - childPaddingTop - childPaddingBottom;

        parentUnpaddedWidth = parentWidth - childPaddingLeft - childPaddingRight;
        parentUnpaddedHeight = parentHeight - childPaddingTop - childPaddingBottom;

        if (parentUnpaddedWidth * unpaddedHeight > parentUnpaddedHeight * unpaddedWidth) {
            // The child view should be left/right letterboxed.
            final int scaledChildWidth = unpaddedWidth * parentUnpaddedHeight
                    / unpaddedHeight + childPaddingLeft + childPaddingRight;
            child.layout(
                    parentLeft + (parentWidth - scaledChildWidth) / 2,
                    parentTop,
                    parentRight - (parentWidth - scaledChildWidth) / 2,
                    parentBottom);
        } else {
            // The child view should be top/bottom letterboxed.
            final int scaledChildHeight = unpaddedHeight * parentUnpaddedWidth
                    / unpaddedWidth + childPaddingTop + childPaddingBottom;
            child.layout(
                    parentLeft,
                    parentTop + (parentHeight - scaledChildHeight) / 2,
                    parentRight,
                    parentTop + (parentHeight + scaledChildHeight) / 2);
        }
    }
}