android.widget.TextView.getMeasuredWidth()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(127)

本文整理了Java中android.widget.TextView.getMeasuredWidth()方法的一些代码示例,展示了TextView.getMeasuredWidth()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TextView.getMeasuredWidth()方法的具体详情如下:
包路径:android.widget.TextView
类名称:TextView
方法名:getMeasuredWidth

TextView.getMeasuredWidth介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

private boolean isTooLarge (TextView text, String newText) {
  float textWidth = text.getPaint().measureText(newText);
  return (textWidth >= text.getMeasuredWidth ());
}

代码示例来源: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);
int widthSoFar = txtSample.getMeasuredWidth();
for (Sample samItem : collection) {
  TextView txtSamItem = new TextView(this, null,
  widthSoFar += txtSamItem.getMeasuredWidth();
    widthSoFar = txtSamItem.getMeasuredWidth();
  } else {
    llAlso.addView(txtSamItem);

代码示例来源:origin: baoyachi/StepView

mTextView.measure(spec, spec);
int measuredWidth = mTextView.getMeasuredWidth();
mTextView.setX(complectedXPosition.get(i) - measuredWidth / 2);
mTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

代码示例来源:origin: rey5137/material

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  TextView tv = getTabView(mSelectedPosition);
  if(tv != null)
    updateIndicator(tv.getLeft(), tv.getMeasuredWidth());
}

代码示例来源:origin: rey5137/material

@Override
public void onPageScrollStateChanged(int state) {
  if(state == ViewPager.SCROLL_STATE_IDLE){
    mScrolling = false;
    TextView tv = getTabView(mSelectedPosition);
    if(tv != null) {
      updateIndicator(tv.getLeft(), tv.getMeasuredWidth());
    }
  }
  else
    mScrolling = true;
  if (mListener != null)
    mListener.onPageScrollStateChanged(state);
}

代码示例来源: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: lipangit/JiaoZiVideoPlayer

clarityPopWindow.showAsDropDown(clarity);
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int offsetX = clarity.getMeasuredWidth() / 3;
int offsetY = clarity.getMeasuredHeight() / 3;
clarityPopWindow.update(clarity, -offsetX, -offsetY, Math.round(layout.getMeasuredWidth() * 2), layout.getMeasuredHeight());

代码示例来源:origin: akexorcist/Android-RoundCornerProgressBar

private void drawTextProgressPosition() {
//        tvProgress.setVisibility(View.INVISIBLE);
//        tvProgress.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @SuppressWarnings("deprecation")
//            @Override
//            public void onGlobalLayout() {
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
//                    tvProgress.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                else
//                    tvProgress.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                setTextProgressAlign();
//            }
//        });
    clearTextProgressAlign();
    // TODO Temporary
    int textProgressWidth = tvProgress.getMeasuredWidth() + (getTextProgressMargin() * 2);
    float ratio = getMax() / getProgress();
    int progressWidth = (int) ((getLayoutWidth() - (getPadding() * 2)) / ratio);
    if (textProgressWidth + textProgressMargin < progressWidth) {
      alignTextProgressInsideProgress();
    } else {
      alignTextProgressOutsideProgress();
    }
  }

代码示例来源: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: stackoverflow.com

float width = view.getMeasuredWidth();
float height = view.getMeasuredHeight();
    + screenWidth + "///" + view.getMeasuredWidth());

代码示例来源: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

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: stackoverflow.com

TextView title = (TextView) view.findViewById(R.id.listitemThreadsTitle);
title.setVisibility(View.VISIBLE);

TextPaint p = title.getPaint();
String strTitle = "Moe, Joe, Isaac, Bethany, Cornelius, Charlie";
title.setText(strTitle);
float avail = title.getMeasuredWidth();
CharSequence ch = TextUtils.commaEllipsize(strTitle, p, avail, "one more", "%d more");
title.setText(ch);

代码示例来源: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: stackoverflow.com

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView edit = (TextView) findViewById(R.id.edit);
  edit.setTextSize(20);       
  edit.setText("Hello, world");       
  edit.measure(0, 0);
  int width = edit.getMeasuredWidth();
  Log.w("width", width.toString());
}

代码示例来源:origin: stackoverflow.com

TextView upTextView = (TextView) getLayoutInflater().inflate(
    R.layout.up_text, null);
upTextView.setText("CITIES");
upTextView.measure(0, 0);
upTextView.layout(0, 0, upTextView.getMeasuredWidth(), 
    upTextView.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(upTextView.getMeasuredWidth(),
    upTextView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
upTextView.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);

代码示例来源:origin: stackoverflow.com

public static void expandCollapsedByMaxLines(@NonNull final TextView text) {
  final int height = text.getMeasuredHeight();
  text.setHeight(height);
  text.setMaxLines(Integer.MAX_VALUE); //expand fully
  text.measure(View.MeasureSpec.makeMeasureSpec(text.getMeasuredWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED));
  final int newHeight = text.getMeasuredHeight();
  ObjectAnimator animation = ObjectAnimator.ofInt(text, "height", height, newHeight);
  animation.setDuration(250).start();
}

相关文章

TextView类方法