下面列出了android.widget.TextView#getMeasuredHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
final int x) {
final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
final CharSequence debugInfo = debugInfoView.getText();
if (debugInfo == null) {
return;
}
placerView.addView(debugInfoView);
debugInfoView.measure(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int infoWidth = debugInfoView.getMeasuredWidth();
final int y = debugInfoView.getMeasuredHeight();
ViewLayoutUtils.placeViewAt(
debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
private int getAdditionalPadding() {
float textSize = getTextSize();
TextView textView = new TextView(getContext());
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
textView.setLines(1);
textView.measure(0, 0);
int measuredHeight = textView.getMeasuredHeight();
if (measuredHeight - textSize > 0) {
mAdditionalPadding = (int) (measuredHeight - textSize);
Log.v("NoPaddingTextView", "onMeasure: height=" + measuredHeight + " textSize=" + textSize + " mAdditionalPadding=" + mAdditionalPadding);
}
return mAdditionalPadding;
}
/**
* 初始化 singleLine 模式需要的视图
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
private void initSingleLineView(int widthMeasureSpec, int heightMeasureSpec) {
if (!mSingleLine) {
return;
}
if (mShowRightImage) {
imageView = new ImageView(getContext());
imageView.setImageResource(mRightImageResId);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
measureChild(imageView, widthMeasureSpec, heightMeasureSpec);
imageWidth = imageView.getMeasuredWidth();
imageHeight = imageView.getMeasuredHeight();
addView(imageView);
}
if (mShowEndText) {
endText = (TextView) mInflater.inflate(mTagResId, null);
if (mTagResId == DEFAULT_TAG_RESID) {
endText.setBackgroundResource(mBackground);
endText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
endText.setTextColor(mTagColor);
}
@SuppressLint("DrawAllocation") LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
endText.setLayoutParams(layoutParams);
endText.setText(endTextString == null || endTextString.equals("") ? DEFAULT_END_TEXT_STRING : endTextString);
measureChild(endText, widthMeasureSpec, heightMeasureSpec);
endTextHeight = endText.getMeasuredHeight();
endTextWidth = endText.getMeasuredWidth();
addView(endText);
endText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (onTagClickListener != null) {
onTagClickListener.onTagClick(-1);
}
}
});
}
}
public int getItemHeight() {
if (_h != 0)
return _h;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TextView item = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
item.measure(0, 0);
_h = item.getMeasuredHeight();
return _h;
}
private static Point getPositionAbove(TextView tipView, RxPopupView rxPopupView,
RxCoordinates anchorViewRxCoordinates, RxCoordinates rootLocation) {
Point point = new Point();
point.x = anchorViewRxCoordinates.left + getXOffset(tipView, rxPopupView);
if (rxPopupView.alignedCenter()) {
AdjustHorizontalCenteredOutOfBounds(tipView, rxPopupView.getRootView(), point, rootLocation);
} else if (rxPopupView.alignedLeft()){
AdjustHorizontalLeftAlignmentOutOfBounds(tipView, rxPopupView.getRootView(), point, anchorViewRxCoordinates, rootLocation);
} else if (rxPopupView.alignedRight()){
AdjustHorizotalRightAlignmentOutOfBounds(tipView, rxPopupView.getRootView(), point, anchorViewRxCoordinates, rootLocation);
}
point.y = anchorViewRxCoordinates.top - tipView.getMeasuredHeight();
return point;
}
/**
* 要伸缩文本的高度 宽度固定的MeasureSpec.EXACTLY 高度MeasureSpec.AT_MOST
*
* @return
*/
public int getShortHeight() {
int measuredWidth = contentTextView.getMeasuredWidth();
TextView copyTextView = new TextView(getContext());
copyTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
copyTextView.setMaxLines(3);//设置默认不展开的情况下显示3行
copyTextView.setLines(3);//文本设置3行 不够3行显示3行的高度
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
copyTextView.measure(widthMeasureSpec, heightMeasureSpec);
return copyTextView.getMeasuredHeight();
}
/**
* Title的高度(更新 应用介绍 权限 这几个字) 宽度是固定的MeasureSpec.EXACTLY
*
* @return
*/
public int getTileHeight() {
int measuredWidth = titleTextView.getMeasuredWidth();
TextView copyTextView = new TextView(getContext());
copyTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
copyTextView.setMaxLines(1);
copyTextView.setLines(1);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);//MeasureSpec.AT_MOST情况下前一个参数设置多少无所谓
copyTextView.measure(widthMeasureSpec, heightMeasureSpec);
return copyTextView.getMeasuredHeight();
}
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
final int x) {
final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
final CharSequence debugInfo = debugInfoView.getText();
if (debugInfo == null) {
return;
}
placerView.addView(debugInfoView);
debugInfoView.measure(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int infoWidth = debugInfoView.getMeasuredWidth();
final int y = debugInfoView.getMeasuredHeight();
ViewLayoutUtils.placeViewAt(
debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
/**
* 要伸缩文本的高度 宽度固定的MeasureSpec.EXACTLY 高度MeasureSpec.AT_MOST
*
* @return
*/
public int getShortHeight() {
int measuredWidth = contentTextView.getMeasuredWidth();
TextView copyTextView = new TextView(getContext());
copyTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 8);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
copyTextView.setMaxLines(3);//设置默认不展开的情况下显示3行
copyTextView.setLines(3);//文本设置3行 不够3行显示3行的高度
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);
copyTextView.measure(widthMeasureSpec, heightMeasureSpec);
return copyTextView.getMeasuredHeight();
}
/**
* Title的高度(更新 应用介绍 权限 这几个字) 宽度是固定的MeasureSpec.EXACTLY
*
* @return
*/
public int getTileHeight() {
int measuredWidth = titleTextView.getMeasuredWidth();
TextView copyTextView = new TextView(getContext());
copyTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY);
copyTextView.setMaxLines(1);
copyTextView.setLines(1);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST);//MeasureSpec.AT_MOST情况下前一个参数设置多少无所谓
copyTextView.measure(widthMeasureSpec, heightMeasureSpec);
return copyTextView.getMeasuredHeight();
}
private static int getTextViewHeight(Context context, String text, float textSize, int deviceWidth, int padding) {
TextView textView = new TextView(context);
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
textView.setPadding(padding, padding, padding, padding);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
final int x) {
final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
final CharSequence debugInfo = debugInfoView.getText();
if (debugInfo == null) {
return;
}
placerView.addView(debugInfoView);
debugInfoView.measure(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int infoWidth = debugInfoView.getMeasuredWidth();
final int y = debugInfoView.getMeasuredHeight();
ViewLayoutUtils.placeViewAt(
debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
/**
* 获取10行文本的高度
* @return
*/
public int getShortMeasureHeight() {
TextView textView = new TextView(UiUtils.getContext());
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
textView.setLines(10);//表示有十行数据
//开始宽度
int width = tv_content.getMeasuredWidth();
//指定测量规则
int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measureHeight = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.AT_MOST);
//开始测量
textView.measure(measureWidth, measureHeight);
return textView.getMeasuredHeight();
}
/**
* 获取文本实际所占高度,辅助用于计算行高,行数
*
* @param text 文本
* @param textSize 字体大小
* @param deviceWidth 屏幕宽度
*/
private void measureTextViewHeight(String text, float textSize, int deviceWidth) {
TextView textView = new TextView(getContext());
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
originalLineCount = textView.getLineCount();
originalHeight = textView.getMeasuredHeight();
}
public static int getHeight(Context context, String text, int deviceWidth) {
TextView textView = new TextView(context);
setPadding(context, textView);
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
public void onDraw(Canvas canvas) {
Paint p = new Paint();
if (this.isSelect) {
this.paintColor = getContext().getResources().getColor(R.color.colorAccent);
this.textBg = R.drawable.x8_ai_follow_marker_info_select_bg;
} else {
this.paintColor = getContext().getResources().getColor(R.color.black_65);
this.textBg = R.drawable.x8_ai_follow_marker_info_bg;
}
p.setColor(this.paintColor);
p.setStrokeWidth(5.0f);
View v1;
View v2;
if (this.type == 1) {
TextView v12 = (TextView) getChildAt(0);
TextView v22 = (TextView) getChildAt(3);
v12.setBackgroundResource(this.textBg);
int startY = v12.getMeasuredHeight();
int endY = getMeasuredHeight() - v22.getMeasuredHeight();
if (this.isRelation) {
v22.setBackgroundResource(R.drawable.x8_ai_follow_marker_info_select_bg);
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) startY, (float) (getMeasuredWidth() / 2), (float) (endY - this.lintTop), p);
p.setColor(getContext().getResources().getColor(R.color.colorAccent));
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) this.lintTop, (float) (getMeasuredWidth() / 2), (float) endY, p);
} else {
v22.setBackgroundResource(this.textBg);
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) startY, (float) (getMeasuredWidth() / 2), (float) endY, p);
}
} else if (this.type == 2) {
v1 = getChildAt(0);
v2 = getChildAt(1);
v1.setBackgroundResource(this.textBg);
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) v1.getMeasuredHeight(), (float) (getMeasuredWidth() / 2), (float) (getMeasuredHeight() - (v2.getMeasuredHeight() / 2)), p);
} else if (this.type == 3) {
v1 = getChildAt(0);
v2 = getChildAt(4);
getChildAt(1).setBackgroundResource(this.textBg);
v2.setBackgroundResource(this.textBg);
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) (v1.getMeasuredHeight() / 2), (float) (getMeasuredWidth() / 2), (float) (getMeasuredHeight() - v2.getMeasuredHeight()), p);
} else if (this.type == 4) {
v1 = getChildAt(0);
v2 = getChildAt(2);
getChildAt(1).setBackgroundResource(this.textBg);
canvas.drawLine((float) (getMeasuredWidth() / 2), (float) (v1.getMeasuredHeight() / 2), (float) (getMeasuredWidth() / 2), (float) (getMeasuredHeight() - (v2.getMeasuredHeight() / 2)), p);
}
super.onDraw(canvas);
}
public static int getTextViewHeight(Context context, boolean isBig, float textSize, float textSizeSelected) {
TextView textView = getTextView(context, isBig, textSize, textSizeSelected);
textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
return textView.getMeasuredHeight();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println("onLayout:" + changed);
if (changed) {
int right = r - getPaddingRight();
int bottom = b - getPaddingBottom();
int top = bottom - mMenuButton.getMeasuredHeight();
mMenuButton.layout(right - mMenuButton.getMeasuredWidth(), top, right, bottom);
int dw = (mMenuButton.getMeasuredWidth() - mIcon.getMeasuredWidth()) / 2;
int dh = (mMenuButton.getMeasuredHeight() - mIcon.getMeasuredHeight()) / 2;
mIcon.layout(right - mIcon.getMeasuredWidth() - dw,
bottom - mIcon.getMeasuredHeight() - dh, right - dw, bottom - dh);
if (isCircle) {
if (mMenuItems.size() < 2) {
Log.e("onLayout", "Floating Action Buttons must more then one!");
return;
}
double angle = Math.PI/2d/(mMenuItems.size() - 1);
for (int i = 0; i < mMenuItems.size(); i++) {
FloatingActionButton itemFB = mMenuItems.get(i);
int fbWidth = itemFB.getMeasuredWidth();
int fbHeight = itemFB.getMeasuredHeight();
if (0 != multipleOfFB) {
mRadius = (int) (fbWidth * multipleOfFB);
}
int itemDw = (mMenuButton.getMeasuredWidth() - fbWidth) / 2;
int itemDh = (mMenuButton.getMeasuredHeight() - fbHeight) / 2;
int itemX = (int) (mRadius*Math.cos(i*angle));
int itemY = (int) (mRadius*Math.sin(i*angle));
itemFB.layout(right - itemX - fbWidth - itemDw, bottom - itemY - fbHeight - itemDh,
right - itemX - itemDw, bottom - itemY - itemDh);
if (!animating) {
if (!mOpen) {
itemFB.setTranslationY(mMenuButton.getTop() - itemFB.getTop());
itemFB.setTranslationX(mMenuButton.getLeft() - itemFB.getLeft());
itemFB.setVisibility(GONE);
} else {
itemFB.setTranslationY(0);
itemFB.setTranslationX(0);
itemFB.setVisibility(VISIBLE);
}
}
}
} else {
for (int i = 0; i < mMenuItems.size(); i++) {
FloatingActionButton item = mMenuItems.get(i);
TextView label = mMenuItemLabels.get(i);
label.setBackgroundResource(R.drawable.rounded_corners);
bottom = top -= mItemGap;
top -= item.getMeasuredHeight();
int width = item.getMeasuredWidth();
int d = (mMenuButton.getMeasuredWidth() - width) / 2;
item.layout(right - width - d, top, right - d, bottom);
d = (item.getMeasuredHeight() - label.getMeasuredHeight()) / 2;
label.layout(item.getLeft() - label.getMeasuredWidth() - 50,
item.getTop() + d, item.getLeft(),
item.getTop() + d + label.getMeasuredHeight());
if (!animating) {
if (!mOpen) {
item.setTranslationY(mMenuButton.getTop() - item.getTop());
item.setVisibility(GONE);
label.setVisibility(GONE);
} else {
item.setTranslationY(0);
item.setVisibility(VISIBLE);
label.setVisibility(VISIBLE);
}
}
}
}
if (!animating && getBackground() != null) {
if (!mOpen) {
getBackground().setAlpha(0);
} else {
getBackground().setAlpha(0xff);
}
}
}
}
@SuppressWarnings("ConstantConditions")
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
// Let our parent(a LinearLayout) measure first
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Check if we're allowed to resize our view's width
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
final boolean resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
// Variables where we'll store our measured width and height
int measuredWidth;
int measuredHeight;
// Retrieve some initial dimensions of our components
final int paddingSides = getResources()
.getDimensionPixelSize(R.dimen.lib_calendar_day_padding_sides);
final int daysInRow = getDaysInRow();
final int dayWidth;
if(resizeWidth) {
// We may resize our View, so use our preferred size
dayWidth = getResources().getDimensionPixelSize(R.dimen.lib_calendar_day_size);
} else {
// We're not allowed to resize our View, so make sure it fits
dayWidth = getAvailableDayWidth(maxWidth);
}
// Calculate our width, based on the width of a single day, the padding and the number of days each week(row)
measuredWidth = (dayWidth * daysInRow) + (paddingSides * daysInRow * 2);
// Calculate a measured height of the headers by using a sample TextView
// First, create a TextView with sample text
final LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final TextView sampleHeader = (TextView)
inflater.inflate(R.layout.lib_calendar_single_header, this, false);
sampleHeader.setText(R.string.lib_header_monday);
// Second, measure the TextView's height
int textWidthMeasureSpec = View.MeasureSpec.makeMeasureSpec(measuredWidth / daysInRow,
View.MeasureSpec.AT_MOST);
int textHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
sampleHeader.measure(textWidthMeasureSpec, textHeightMeasureSpec);
final int headerHeight = sampleHeader.getMeasuredHeight();
// Calculate the height of the weeks
final int weeksInMonth;
if(this instanceof CalendarView) {
// If we're a single month, calculate the actual height
weeksInMonth = mFirstValidDay.getActualMaximum(Calendar.WEEK_OF_MONTH);
} else {
weeksInMonth = MAX_WEEKS_IN_MONTH;
}
final int weekHeight = dayWidth * weeksInMonth + (paddingSides * (weeksInMonth + 1) * 2);
// The height is the height of all the weeks, plus the headers
measuredHeight = weekHeight + headerHeight;
// If we're MultiCalendarView, also add the height of the TitlePageIndicator
if(this instanceof MultiCalendarView) {
final MultiCalendarView multiCalendarView = (MultiCalendarView) this;
final TitlePageIndicator indicator = multiCalendarView.getIndicator();
// Use the width we measured, and let the Indicator calculate it's height
int indicatorWidthMeasureSpec = View.MeasureSpec.makeMeasureSpec(
resolveSize(measuredWidth, widthMeasureSpec), MeasureSpec.EXACTLY);
int indicatorHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
indicator.measure(indicatorWidthMeasureSpec, indicatorHeightMeasureSpec);
final int indicatorHeight = indicator.getMeasuredHeight();
// Now we have the height, update our ViewPager with the new dimensions
indicatorHeightMeasureSpec = View.MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY);
final ViewPager viewPager = multiCalendarView.getViewPager();
viewPager.measure(indicatorWidthMeasureSpec, indicatorHeightMeasureSpec);
// Add the indicator height to the measured height
measuredHeight += indicatorHeight;
}
// Set the measured dimensions
setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec),
resolveSize(measuredHeight, heightMeasureSpec));
}
/**
* *
* Method for Setting the Height of the ListView dynamically. Hack to fix
* the issue of not showing all the items of the ListView when placed inside
* a ScrollView
* **
*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
long numOfLines = 1;
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
TextView file = (TextView) view.findViewById(R.id.name);
TextView percentage = (TextView) view.findViewById(R.id.percentage);
ProgressBar progressBar1 = (ProgressBar) view.findViewById(R.id.progressBar1);
if (view.getMeasuredWidth() > desiredWidth) {
double viewWidthLong = Double.valueOf(view.getMeasuredWidth());
double desiredWidthLong = Double.valueOf(desiredWidth);
numOfLines = Math.round(viewWidthLong / desiredWidthLong) + 1;
totalHeight += (file.getMeasuredHeight() * numOfLines) + percentage.getMeasuredHeight() + progressBar1.getMeasuredHeight();
} else {
totalHeight += view.getMeasuredHeight();
}
}
LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}