String dt = time + " " + date;
Rect bounds = new Rect();
TextPaint paint = getPaint();
paint.getTextBounds(dt, 0, dt.length(), bounds);
mTextWidth = bounds.width();你上面这段代码是在TextView的子类里面写的吗?TextView mTextView;
//首先已经执行了setText
String dt = mTextView.getText().toString();
Rect bounds = new Rect();
TextPaint paint = mTextView.getPaint();
paint.getTextBounds(dt, 0, dt.length(), bounds);
mTextWidth = bounds.width();
Rect bounds = new Rect();
TextPaint paint = getPaint();
paint.getTextBounds(dt, 0, dt.length(), bounds);
mTextWidth = bounds.width();你上面这段代码是在TextView的子类里面写的吗?TextView mTextView;
//首先已经执行了setText
String dt = mTextView.getText().toString();
Rect bounds = new Rect();
TextPaint paint = mTextView.getPaint();
paint.getTextBounds(dt, 0, dt.length(), bounds);
mTextWidth = bounds.width();
我后来是这样想的,我发现Spannable WordtoSpan = new SpannableString(time + " " + date);
WordtoSpan.setSpan(new AbsoluteSizeSpan(mTextSize_big), 0, time.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new AbsoluteSizeSpan(mTextSize_small), time.length() + 1, time.length() + date.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new StyleSpan(Typeface.BOLD), 0, time.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
WordtoSpan.setSpan(new TypefaceSpan("sans"), 0, time.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setText(WordtoSpan);setText后,测出来的String宽度是textView默认textsize得到的宽度,那我真实的长度这样就没法得到了...
然后我就只能做复杂点了,setTextSize(mTextSize_big);Rect bounds = new Rect();
TextPaint paint = getPaint();
paint.getTextBounds(time, 0, time.length(), bounds);
mTextWidth = bounds.width();
[code=javasetTextSize(mTextSize_small);]Rect bounds = new Rect();
TextPaint paint = getPaint();
paint.getTextBounds(date, 0, date.length(), bounds);
mTextWidth = bounds.width();[/code]
把得到的两个width加起来就是我要的String的长度了,然后再通过和textView的宽度比较是减小字体还是这样就行了。语言组织力不强,看了我要达到的目的,你们有没有更简单点的方法啊?