所以我找到了这个问题的答案,把它贴在这里来帮助下一个人。
我创建了一个帮助类来包含调整跨度的方法,您可以使用以下语法调用它(这是将最后 2 个字符设置为在行上显示更高):
SpannableString contentAmount = new SpannableString(amount);
contentAmount.setSpan(new SuperscriptSpanAdjuster(3.0/5.0), contentAmount.length() - 2, contentAmount.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
助手类是:
/**
* This is a helper class to help adjust the alignment of a section of text, when using SpannableStrings to set text
* formatting dynamically.
*
*/
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class SuperscriptSpanAdjuster extends MetricAffectingSpan {
double ratio = 0.5;
public SuperscriptSpanAdjuster() {
}
public SuperscriptSpanAdjuster(double ratio) {
this.ratio = ratio;
}
@Override
public void updateDrawState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}
@Override
public void updateMeasureState(TextPaint paint) {
paint.baselineShift += (int) (paint.ascent() * ratio);
}
}