我相信您必须实现自定义Span,只需将updateMeasureState保留为空:)尝试使用
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class TestSubScriptSpan extends MetricAffectingSpan {
private final float mProportion;
public TestSubScriptSpan(float proportion) {
mProportion = proportion;
}
@Override
public void updateMeasureState(TextPaint p) {
// DO NOTHING!
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTextSize(tp.getTextSize() * mProportion);
tp.baselineShift -= (int) (tp.ascent() / 2);
}
}
然后
TestSubScriptSpan tss = new TestSubScriptSpan(2.0f);
s.setSpan(tss, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(tss, s.length() - 1, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
注意你在updateDrawState()中所做的事情的顺序,因为它很重要.