本文整理了Java中android.widget.TextView.measure()
方法的一些代码示例,展示了TextView.measure()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TextView.measure()
方法的具体详情如下:
包路径:android.widget.TextView
类名称:TextView
方法名:measure
暂无
代码示例来源:origin: stackoverflow.com
public static int getHeight(Context context, CharSequence text, int textSize, int deviceWidth, Typeface typeface,int padding) {
TextView textView = new TextView(context);
textView.setPadding(padding,0,padding,padding);
textView.setTypeface(typeface);
textView.setText(text, TextView.BufferType.SPANNABLE);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
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();
}
代码示例来源:origin: stackoverflow.com
public static int getHeight(Context context, String text, int textSize, int deviceWidth) {
TextView textView = new TextView(context);
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
代码示例来源:origin: jaydenxiao2016/AndroidFire
private static int getTextViewHeight(TextView textView, int width) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
代码示例来源:origin: seven332/EhViewer
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (MeasureSpec.EXACTLY != widthMode || MeasureSpec.EXACTLY != heightMode) {
throw new IllegalStateException();
}
switch (mStep) {
case 0:
mLeftText.measure(MeasureSpec.makeMeasureSpec(widthSize / 3, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY));
mRightText.measure(MeasureSpec.makeMeasureSpec(widthSize / 3, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY));
mMenuText.measure(MeasureSpec.makeMeasureSpec(widthSize / 3, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(heightSize / 2, MeasureSpec.EXACTLY));
mProgressText.measure(MeasureSpec.makeMeasureSpec(widthSize / 3, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(heightSize / 2, MeasureSpec.EXACTLY));
break;
default:
case 1:
mLongClickText.measure(MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY));
break;
}
setMeasuredDimension(widthSize, heightSize);
}
代码示例来源:origin: stackoverflow.com
view.measure(widthSpec, heightSpec);
代码示例来源:origin: qiujuer/Genius-Android
public void resetSizes(String maxValue) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//Account for negative numbers... is there any proper way of getting the biggest string between our range????
mNumber.setText(String.format("-%s", maxValue));
//Do a first forced measure call for the TextView (with the biggest text content),
//to calculate the max width and use always the same.
//this avoids the TextView from shrinking and growing when the text content changes
int wSpec = MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, MeasureSpec.AT_MOST);
int hSpec = MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST);
mNumber.measure(wSpec, hSpec);
mWidth = Math.max(mNumber.getMeasuredWidth(), mNumber.getMeasuredHeight());
removeView(mNumber);
addView(mNumber, new FrameLayout.LayoutParams(mWidth, mWidth, Gravity.LEFT | Gravity.TOP));
}
代码示例来源:origin: rey5137/material
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int nonTextWidth = mAvatarSize + mSpacing * 3 + mButtonSize;
int ws = MeasureSpec.makeMeasureSpec(widthSize - nonTextWidth, widthMode == MeasureSpec.UNSPECIFIED ? widthMode : MeasureSpec.AT_MOST);
int hs = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
mNameView.measure(ws, hs);
mAddressView.measure(ws, hs);
if(mButton != null)
mButton.measure(MeasureSpec.makeMeasureSpec(mButtonSize, MeasureSpec.EXACTLY), hs);
int width = widthMode == MeasureSpec.EXACTLY ? widthSize : Math.max(mNameView.getMeasuredWidth(), mAddressView.getMeasuredWidth()) + nonTextWidth;
int height = Math.max(mAvatarSize + mSpacing * 2, mNameView.getMeasuredHeight() + mAddressView.getMeasuredHeight());
switch (heightMode){
case MeasureSpec.EXACTLY:
height = heightSize;
break;
case MeasureSpec.AT_MOST:
height = Math.min(height, heightSize);
break;
}
height = Math.max(mMinHeight, height);
if(mButton != null)
mButton.measure(MeasureSpec.makeMeasureSpec(mButtonSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
setMeasuredDimension(width, height);
}
代码示例来源:origin: stackoverflow.com
txtSample.measure(0, 0);
txtSamItem.measure(0, 0);
widthSoFar += txtSamItem.getMeasuredWidth();
代码示例来源:origin: baoyachi/StepView
mTextView.setText(mStepBeanList.get(i).getName());
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mTextView.measure(spec, spec);
代码示例来源:origin: jaydenxiao2016/AndroidFire
private void initView() {
RelativeLayout layout = new RelativeLayout(mContext);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mGood = new TextView(mContext);
mGood.setIncludeFontPadding(false);
mGood.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
mGood.setTextColor(ContextCompat.getColor(mContext, mTextColor));
mGood.setText(mText);
mGood.setLayoutParams(params);
layout.addView(mGood);
setContentView(layout);
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mGood.measure(w, h);
setWidth(mGood.getMeasuredWidth());
setHeight(mDistance + mGood.getMeasuredHeight());
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setFocusable(false);
setTouchable(false);
setOutsideTouchable(false);
mAnimationSet = createAnimation();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void onTouchEvent_shouldCallMovementMethodOnTouchEventWithSetMotionEvent() throws Exception {
TestMovementMethod testMovementMethod = new TestMovementMethod();
textView.setMovementMethod(testMovementMethod);
textView.setLayoutParams(new FrameLayout.LayoutParams(100, 100));
textView.measure(100, 100);
MotionEvent event = MotionEvent.obtain(0, 0, 0, 0, 0, 0);
textView.dispatchTouchEvent(event);
assertEquals(testMovementMethod.event, event);
}
代码示例来源:origin: venshine/GoodView
private static int getTextViewHeight(TextView textView, int width) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredHeight();
}
代码示例来源:origin: google/santa-tracker-android
public static void fitToBounds(TextView textView, float widthPx, float heightPx) {
textView.measure(0, 0);
float currentWidthPx = textView.getMeasuredWidth();
float currentHeightPx = textView.getMeasuredHeight();
float textSize = textView.getTextSize();
float scale = Math.min(widthPx / currentWidthPx, heightPx / currentHeightPx);
textView.setTextSize(textSize * scale);
}
代码示例来源:origin: venshine/GoodView
private void initView() {
RelativeLayout layout = new RelativeLayout(mContext);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mGood = new TextView(mContext);
mGood.setIncludeFontPadding(false);
mGood.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
mGood.setTextColor(mTextColor);
mGood.setText(mText);
mGood.setLayoutParams(params);
layout.addView(mGood);
setContentView(layout);
int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
mGood.measure(w, h);
setWidth(mGood.getMeasuredWidth());
setHeight(mDistance + mGood.getMeasuredHeight());
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
setFocusable(false);
setTouchable(false);
setOutsideTouchable(false);
mAnimationSet = createAnimation();
}
代码示例来源:origin: stackoverflow.com
view.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
代码示例来源:origin: stackoverflow.com
tv.measure( 0, 0 );
int tvSize = tv.getMeasuredWidth();
代码示例来源:origin: Meituan-Dianping/Shield
public static int getTextViewWidth(TextView textView, String text, int textSize) {
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredWidth();
}
代码示例来源:origin: Meituan-Dianping/Shield
public static int getTextViewWidth(TextView textView, String text) {
textView.setText(text);
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return textView.getMeasuredWidth();
}
代码示例来源:origin: LemonITCN/LemonBubble4Android
/**
* 获取指定的textV的行高
*
* @param textView 要获取的textView的行高
* @return textView的每行的高度
*/
int getTitleHeight(TextView textView, int viewWidth) {
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(_PST.dpToPx(viewWidth), View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
textView.measure(widthMeasureSpec, heightMeasureSpec);
return _PST.pxToDp(textView.getMeasuredHeight());
}
代码示例来源:origin: DroidsOnRoids/Workcation
private static void setTextViewData(TextView view, TextResizeData data, float fontSize) {
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
view.setPadding(data.paddingLeft, data.paddingTop, data.paddingRight, data.paddingBottom);
view.setRight(view.getLeft() + data.width);
view.setBottom(view.getTop() + data.height);
view.setTextColor(data.textColor);
int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY);
view.measure(widthSpec, heightSpec);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}
内容来源于网络,如有侵权,请联系作者删除!