下面列出了android.view.View#getMeasuredHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void measureScrapChild(RecyclerView.Recycler recycler, int position, int widthSpec,
int heightSpec, int[] measuredDimension) {
try {
View view = recycler.getViewForPosition(position);//fix 动态添加时报IndexOutOfBoundsException
if (view != null) {
RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) view.getLayoutParams();
int childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec,
getPaddingLeft() + getPaddingRight(), p.width);
int childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec,
getPaddingTop() + getPaddingBottom(), p.height);
view.measure(childWidthSpec, childHeightSpec);
measuredDimension[0] = view.getMeasuredWidth() + p.leftMargin + p.rightMargin;
measuredDimension[1] = view.getMeasuredHeight() + p.bottomMargin + p.topMargin;
recycler.recycleView(view);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final View child = getChildAt(0);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
mLeft = paddingLeft + lp.leftMargin;
mRight = child.getMeasuredWidth() + mLeft;
mTop = paddingTop + lp.topMargin + mOffset;
mBottom = child.getMeasuredHeight() + mTop;
child.layout(mLeft, mTop, mRight, mBottom);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
final int width = r - l;
int xPos = getPaddingLeft();
int yPos = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final int childWidth = child.getMeasuredWidth();
final int childHeight = child.getMeasuredHeight();
final FlowLayoutParams lp = (FlowLayoutParams) child.getLayoutParams();
if (xPos + childWidth > width) {
xPos = getPaddingLeft();
yPos += lineHeight;
}
child.layout(xPos, yPos, xPos + childWidth, yPos + childHeight);
xPos += childWidth + lp.hSpacing;
}
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
final int parentLeft = getPaddingLeft();
final int parentTop = getPaddingTop();
int lastBottom = parentTop;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
int childLeft = parentLeft + lp.leftMargin;
int childTop = lastBottom + lp.topMargin;
child.layout(childLeft, childTop, childLeft + width, childTop + height);
lastBottom = childTop + height + lp.bottomMargin;
}
}
}
public int getRowHeight() {
if (mRowHeight > 0) {
return mRowHeight;
}
ListAdapter adapter = getAdapter();
int numColumns = getNumColumnsCompatible();
// adapter has not been set or has no views in it;
if (adapter == null || adapter.getCount() <= numColumns * (mHeaderViewInfos.size() + mFooterViewInfos.size())) {
return -1;
}
int mColumnWidth = getColumnWidthCompatible();
View view = getAdapter().getView(numColumns * mHeaderViewInfos.size(), mViewForMeasureRowHeight, this);
AbsListView.LayoutParams p = (AbsListView.LayoutParams) view.getLayoutParams();
if (p == null) {
p = new AbsListView.LayoutParams(-1, -2, 0);
view.setLayoutParams(p);
}
int childHeightSpec = getChildMeasureSpec(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);
int childWidthSpec = getChildMeasureSpec(
MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
view.measure(childWidthSpec, childHeightSpec);
mViewForMeasureRowHeight = view;
mRowHeight = view.getMeasuredHeight();
return mRowHeight;
}
/**
* Refresh the listview height because the listviews are displayed in a scrollview.
* @param listView the listview.
*/
private void refreshListViewHeight(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
private Point getActionViewCoordinates() {
int[] coordinates = new int[2];
// This method returns a x and y values that can be larger than the dimensions of the device screen.
getLocationOnScreen(coordinates);
// We then need to deduce the offsets
Rect activityFrame = new Rect();
View v = Utils.getActivityContentView(context);
if (v != null) {
v.getWindowVisibleDisplayFrame(activityFrame);
coordinates[0] -= (Utils.getScreenSize(context).x - v.getMeasuredWidth());
coordinates[1] -= (activityFrame.height() + activityFrame.top - v.getMeasuredHeight());
}
return new Point(coordinates[0], coordinates[1]);
}
/**
* Layout a child that has been measured, preserving its top position.
* TODO: unify with setUpChild.
* @param child The child.
*/
private void relayoutMeasuredItem(View child) {
final int w = child.getMeasuredWidth();
final int h = child.getMeasuredHeight();
final int childLeft = mListPadding.left;
final int childRight = childLeft + w;
final int childTop = child.getTop();
final int childBottom = childTop + h;
child.layout(childLeft, childTop, childRight, childBottom);
}
@Override
public void transform(View view, float position) {
int width = view.getMeasuredWidth(), height = view.getMeasuredHeight();
view.setTranslationX(width * position * translationXRate * (2f / (Math.abs(position) + 2)));
view.setScaleX(2f / (position + 2));
view.setScaleY(2f / (position + 2));
view.setAlpha(position < 0 ? Math.max(1 + position, 0) : 1);
}
private int measureHeight() {
// get ListView adapter
ListAdapter adapter = mListView.getAdapter();
if (null == adapter) {
return 0;
}
int totalHeight = 0;
for (int i = 0, len = adapter.getCount(); i < len; i++) {
View item = adapter.getView(i, null, mListView);
if (null == item) continue;
// measure each item width and height
item.measure(0, 0);
// calculate all height
totalHeight += item.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mListView.getLayoutParams();
if (null == params) {
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
// calculate ListView height
params.height = totalHeight + (mListView.getDividerHeight() * (adapter.getCount() - 1));
mListView.setLayoutParams(params);
return params.height;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
if (mHeight < 0) {
mHeight = 0;
}
setMeasuredDimension(width, mHeight);
final View childView = getChildView();
if (childView != null) {
childView.measure(widthMeasureSpec, heightMeasureSpec);
mUpdateHeight = childView.getMeasuredHeight();
/* SLog.v(TAG, "mUpdateHeight"+mUpdateHeight); */
}
}
private int getChildTop(View child, int alignmentHeight) {
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int childHeight = child.getMeasuredHeight();
final int alignmentOffset = alignmentHeight > 0 ? (childHeight - alignmentHeight) / 2 : 0;
switch (getChildVerticalGravity(lp.gravity)) {
case Gravity.TOP:
return getPaddingTop() - alignmentOffset;
case Gravity.BOTTOM:
return getHeight() - getPaddingBottom() - childHeight
- lp.bottomMargin - alignmentOffset;
default:
case Gravity.CENTER_VERTICAL:
final int paddingTop = getPaddingTop();
final int paddingBottom = getPaddingBottom();
final int height = getHeight();
final int space = height - paddingTop - paddingBottom;
int spaceAbove = (space - childHeight) / 2;
if (spaceAbove < lp.topMargin) {
spaceAbove = lp.topMargin;
} else {
final int spaceBelow = height - paddingBottom - childHeight -
spaceAbove - paddingTop;
if (spaceBelow < lp.bottomMargin) {
spaceAbove = Math.max(0, spaceAbove - (lp.bottomMargin - spaceBelow));
}
}
return paddingTop + spaceAbove;
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
final int parentLeft = getPaddingLeft();
final int parentTop = getPaddingTop();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
int childLeft = parentLeft;
int childTop = parentTop;
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
switch (orientation) {
case LEFT:
childLeft += triangleHeight;
break;
case TOP:
childTop += triangleHeight;
break;
default:
break;
}
if (!clipToRadius) {
childLeft += radius;
childTop += radius;
}
childLeft += Math.round(borderPaintSize);
childTop += Math.round(borderPaintSize);
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
childLeft += lp.leftMargin;
childTop += lp.topMargin;
child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
boolean widthUnspecified = MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED;
int vertialPaddings = getPaddingTop() + getPaddingBottom();
int horizontalPaddings = getPaddingLeft() + getPaddingRight();
maxWidth -= horizontalPaddings;
int minWidth = 0;
int minHeight = 0;
int lineWidth = 0;
int lineHeight = 0;
int horizontalSpacing = this.horizontalSpacing;
int verticalSpacing = this.verticalSpacing;
int count = getChildCount();
int childWidthMeasureSpec = widthUnspecified ? MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
: MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams layoutParams = child.getLayoutParams();
boolean measure = child.getVisibility() != View.GONE;
if (measure) {
int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, vertialPaddings,
layoutParams.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
int childWidth = child.getMeasuredWidth();
if (lineWidth > 0 && childWidth + lineWidth + horizontalSpacing > maxWidth) {
minWidth = Math.max(lineWidth, minWidth);
minHeight += lineHeight + verticalSpacing;
lineHeight = child.getMeasuredHeight();
lineWidth = childWidth;
} else {
if (lineWidth > 0) {
lineWidth += horizontalSpacing;
}
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, child.getMeasuredHeight());
}
}
if (i + 1 == count) {
minWidth = Math.max(lineWidth, minWidth);
minHeight += lineHeight;
}
}
minWidth += horizontalPaddings;
minHeight += vertialPaddings;
minWidth = Math.max(minWidth, getSuggestedMinimumWidth());
minHeight = Math.max(minHeight, getSuggestedMinimumHeight());
setMeasuredDimension(resolveSize(minWidth, widthMeasureSpec), resolveSize(minHeight, heightMeasureSpec));
}
@Override
public void layoutHeaderView(@NonNull IRefreshView<IIndicator> header) {
final View child = header.getView();
if (mLayout.isDisabledRefresh() || child.getMeasuredHeight() == 0) {
child.layout(0, 0, 0, 0);
if (SmoothRefreshLayout.sDebug) {
Log.d(TAG, String.format("onLayout(): header: %s %s %s %s", 0, 0, 0, 0));
}
return;
}
final SmoothRefreshLayout.LayoutParams lp =
(SmoothRefreshLayout.LayoutParams) child.getLayoutParams();
final IIndicator indicator = mLayout.getIndicator();
int left, right, top = 0, bottom;
switch (header.getStyle()) {
case IRefreshView.STYLE_DEFAULT:
if (mLayout.isMovingHeader()) {
child.setTranslationY(indicator.getCurrentPos());
} else {
child.setTranslationY(0);
}
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
break;
case IRefreshView.STYLE_SCALE:
case IRefreshView.STYLE_PIN:
child.setTranslationY(0);
top = mLayout.getPaddingTop() + lp.topMargin;
break;
case IRefreshView.STYLE_FOLLOW_SCALE:
if (mLayout.isMovingHeader()) {
if (indicator.getCurrentPos() <= indicator.getHeaderHeight()) {
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
child.setTranslationY(indicator.getCurrentPos());
} else {
top = mLayout.getPaddingTop() + lp.topMargin;
child.setTranslationY(0);
}
} else {
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
child.setTranslationY(0);
}
break;
case IRefreshView.STYLE_FOLLOW_PIN:
if (mLayout.isMovingHeader()) {
child.setTranslationY(
Math.min(indicator.getCurrentPos(), indicator.getHeaderHeight()));
} else {
child.setTranslationY(0);
}
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
break;
case IRefreshView.STYLE_FOLLOW_CENTER:
if (mLayout.isMovingHeader()) {
if (indicator.getCurrentPos() <= indicator.getHeaderHeight()) {
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
child.setTranslationY(indicator.getCurrentPos());
} else {
top =
(int)
(mLayout.getPaddingTop()
+ lp.topMargin
+ (indicator.getCurrentPos()
- indicator.getHeaderHeight())
/ 2f);
child.setTranslationY(0);
}
} else {
top = mLayout.getPaddingTop() - child.getMeasuredHeight() - lp.bottomMargin;
child.setTranslationY(0);
}
break;
}
left = mLayout.getPaddingLeft() + lp.leftMargin;
right = left + child.getMeasuredWidth();
if (mLayout.isInEditMode()) top = top + child.getMeasuredHeight();
bottom = top + child.getMeasuredHeight();
child.layout(left, top, right, bottom);
if (SmoothRefreshLayout.sDebug) {
Log.d(TAG, String.format("onLayout(): header: %s %s %s %s", left, top, right, bottom));
}
}
/**
* Update the backgrounds for the buttons to account for their current positioning.
* The primary and secondary buttons are special-cased in that their backgrounds change to
* create the illusion of a single-stroke boundary between them.
*/
private void updateBackgroundsForButtons() {
boolean bothButtonsExist = findViewById(R.id.button_primary) != null
&& findViewById(R.id.button_secondary) != null;
for (int row = 0; row < mIndicesOfRows.size() - 1; row++) {
final int rowStart = mIndicesOfRows.get(row);
final int rowEnd = mIndicesOfRows.get(row + 1);
final int rowSize = rowEnd - rowStart;
for (int i = rowStart; i < rowEnd; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == View.GONE || !isButton(child)) continue;
// Determine which background we need to show.
int background;
if (row == 0) {
// Button will be floating.
background = mBackgroundFloating;
} else if (rowSize == 1 || !bothButtonsExist) {
// Button takes up the full width of the screen.
background = mBackgroundFullRight;
} else if (mLayoutRTL) {
// Primary button will be to the left of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullLeft : mBackgroundFullRight;
} else {
// Primary button will be to the right of the secondary.
background = child.getId() == R.id.button_primary
? mBackgroundFullRight : mBackgroundFullLeft;
}
// Update the background.
LayoutParams params = (LayoutParams) child.getLayoutParams();
if (params.background != background) {
params.background = background;
// Save the padding; Android decides to overwrite it on some builds.
int paddingLeft = child.getPaddingLeft();
int paddingTop = child.getPaddingTop();
int paddingRight = child.getPaddingRight();
int paddingBottom = child.getPaddingBottom();
int buttonWidth = child.getMeasuredWidth();
int buttonHeight = child.getMeasuredHeight();
// Set the background, then restore the padding.
child.setBackgroundResource(background);
child.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
// Re-measuring is necessary to correct the text gravity.
int specWidth = MeasureSpec.makeMeasureSpec(buttonWidth, MeasureSpec.EXACTLY);
int specHeight = MeasureSpec.makeMeasureSpec(buttonHeight, MeasureSpec.EXACTLY);
measureChild(child, specWidth, specHeight);
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
int width = MeasureSpec.getSize(widthMeasureSpec);
int maxWidth = width - AndroidUtilities.dp(32);
int currentLineWidth = 0;
int y = AndroidUtilities.dp(12);
int allCurrentLineWidth = 0;
int allY = AndroidUtilities.dp(12);
int x;
for (int a = 0; a < count; a++) {
View child = getChildAt(a);
if (!(child instanceof GroupCreateSpan)) {
continue;
}
child.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(32), MeasureSpec.EXACTLY));
if (child != removingSpan && currentLineWidth + child.getMeasuredWidth() > maxWidth) {
y += child.getMeasuredHeight() + AndroidUtilities.dp(12);
currentLineWidth = 0;
}
if (allCurrentLineWidth + child.getMeasuredWidth() > maxWidth) {
allY += child.getMeasuredHeight() + AndroidUtilities.dp(12);
allCurrentLineWidth = 0;
}
x = AndroidUtilities.dp(16) + currentLineWidth;
if (!animationStarted) {
if (child == removingSpan) {
child.setTranslationX(AndroidUtilities.dp(16) + allCurrentLineWidth);
child.setTranslationY(allY);
} else if (removingSpan != null) {
if (child.getTranslationX() != x) {
animators.add(ObjectAnimator.ofFloat(child, "translationX", x));
}
if (child.getTranslationY() != y) {
animators.add(ObjectAnimator.ofFloat(child, "translationY", y));
}
} else {
child.setTranslationX(x);
child.setTranslationY(y);
}
}
if (child != removingSpan) {
currentLineWidth += child.getMeasuredWidth() + AndroidUtilities.dp(9);
}
allCurrentLineWidth += child.getMeasuredWidth() + AndroidUtilities.dp(9);
}
int minWidth;
if (AndroidUtilities.isTablet()) {
minWidth = AndroidUtilities.dp(530 - 32 - 18 - 57 * 2) / 3;
} else {
minWidth = (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) - AndroidUtilities.dp(32 + 18 + 57 * 2)) / 3;
}
if (maxWidth - currentLineWidth < minWidth) {
currentLineWidth = 0;
y += AndroidUtilities.dp(32 + 12);
}
if (maxWidth - allCurrentLineWidth < minWidth) {
allY += AndroidUtilities.dp(32 + 12);
}
editText.measure(MeasureSpec.makeMeasureSpec(maxWidth - currentLineWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(AndroidUtilities.dp(32), MeasureSpec.EXACTLY));
if (!animationStarted) {
int currentHeight = allY + AndroidUtilities.dp(32 + 12);
int fieldX = currentLineWidth + AndroidUtilities.dp(16);
fieldY = y;
if (currentAnimation != null) {
int resultHeight = y + AndroidUtilities.dp(32 + 12);
if (containerHeight != resultHeight) {
animators.add(ObjectAnimator.ofInt(InviteContactsActivity.this, "containerHeight", resultHeight));
}
if (editText.getTranslationX() != fieldX) {
animators.add(ObjectAnimator.ofFloat(editText, "translationX", fieldX));
}
if (editText.getTranslationY() != fieldY) {
animators.add(ObjectAnimator.ofFloat(editText, "translationY", fieldY));
}
editText.setAllowDrawCursor(false);
currentAnimation.playTogether(animators);
currentAnimation.start();
animationStarted = true;
} else {
containerHeight = currentHeight;
editText.setTranslationX(fieldX);
editText.setTranslationY(fieldY);
}
} else if (currentAnimation != null) {
if (!ignoreScrollEvent && removingSpan == null) {
editText.bringPointIntoView(editText.getSelectionStart());
}
}
setMeasuredDimension(width, containerHeight);
}
private int getChildHeight(final View child) {
return child.getMeasuredHeight();
}
/**
* Returns the height of the child view taking into account the
* ITEM_VERTICAL_SPACE
*
* @param child The child view
* @return The height of the child view
*/
private int getChildHeight(final View child) {
return child.getMeasuredHeight() + 2 * getChildMargin(child);
}