如何使用TextAppearanceSpan

时间:2013-05-17 12:04:16

标签: android textview

我可以制作较小的最后2个数字,但我希望它们显示如下图所示。

TextView TV = (TextView)findViewById(R.id.txt);

Spannable WordtoSpan = new SpannableString("13.500,27");        

WordtoSpan.setSpan( new TextAppearanceSpan(null, 0, 12, null, null), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
TV.setText(WordtoSpan);

谢谢塔尔

enter image description here

1 个答案:

答案 0 :(得分:4)

修改

您可以使用SuperscriptSpan()

Spannable WordtoSpan = new SpannableString("13.500,27");        
WordtoSpan.setSpan( new TextAppearanceSpan(null, 0, 17, null, null), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
WordtoSpan.setSpan( new SuperscriptSpan(), WordtoSpan.length()-2, WordtoSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  

增加字体大小以查看效果

enter image description here


试试这个 我没有找到任何其他方法,但将上标文本附加到原始的spannalbe字符串

应用跨越SpannableStringBuilder的所需内容

  SpannableStringBuilder builder=new SpannableStringBuilder("13.500,");
  builder.append(Html.fromHtml("X<sup>27</sup>"))

修改2

如果你创建如下所示的自定义范围,它绝对将supertext与top对齐。但是如果你使用默认的SuperscriptSpan,它就无法使文本成为顶级。

public class SuperscriptSpan2 extends MetricAffectingSpan implements
            ParcelableSpan {
        public SuperscriptSpan2() {
        }

    public int getSpanTypeId() {
        return TextUtils.CAP_MODE_CHARACTERS;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.baselineShift += (int) (tp.ascent() / 1.25);
    }

    @Override
    public void updateMeasureState(TextPaint tp) {
        tp.baselineShift += (int) (tp.ascent() / 1.25);
    }
}