标和上标为同一字中的TextView? [英] subscript and superscript for the same word in the TextView?

查看:193
本文介绍了标和上标为同一字中的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法来标和上标为同一词在TextView中我想显示如下文字:

Is there a way to subscript and superscript for the same word in the TextView?I want to display a text that looks like this:

所以,我想这code:

So,I tried this code:

TextView tv1 = ... ;
String latex = "\u222E";
tv1.setText(Html.fromHtml(latex + "<sup>200</sup><sub>2</sub> f(x)dx"));

但结果是这样的:

But the result is this:

您可以看到标出现subscript.How后,我可以解决这个问题?我能解决它的 SpannableString ?或者,我可以改变下标位置?

You can see the superscript appears after the subscript.How I can solve this problem?Can I solve it with SpannableString? Or can I change the position of subscript?

推荐答案

这个自定义范围可以帮助你了解基础的想法:

this custom span can help you understand the base idea:

class SupSubSpan extends ReplacementSpan {
    private String sup;
    private String sub;

    public SupSubSpan(String sup, String sub) {
        this.sup = sup;
        this.sub = sub;
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm) {
        return (int) Math.max(paint.measureText(sup), paint.measureText(sub));
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        canvas.drawText(sup, x, y + paint.ascent() / 2, paint);
        canvas.drawText(sub, x, y - paint.ascent() / 2, paint);
    }
}

这篇关于标和上标为同一字中的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆