如何创建垂直对齐标和下标在TextView中 [英] How to create vertically aligned superscript and subscript in TextView

查看:154
本文介绍了如何创建垂直对齐标和下标在TextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的图片的例子:

我怎样才能使双方的标和下标数字对齐,以产生一个共同的科学记数法像下面

How can I make both the superscript and subscript numbers to be aligned to produce a common scientific notation like below

的TextView ?如果使用方法 Spannable ReplacementSpan 我想看到一个工作的例子。谢谢你。

in TextView? If there is a way using Spannable or ReplacementSpan I would like to see a working example. Thank you.

推荐答案

您可能想尝试这样的事情。

You might want to try something like this.

这是basicall一个 ReplacementSpan 这需要它的应用上的文字,将其分为两部分,并吸引他们在画布上。规模因素和y的翻译有些钦点。我希望这是有益的(或至少是你或其他人可以建立就可以了)。

It's basicall a ReplacementSpan that takes the text it's applied on, separates it into two parts, and draws them on the canvas. The size factor and y translation are somewhat hand-picked. I hope it's useful (or at least that you or someone else can build on it).

public class SuperSubSpan extends ReplacementSpan
{
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, FontMetricsInt fm)
    {
        text = text.subSequence(start, end);
        String[] parts = text.toString().split(",");

        Paint p = getSuperSubPaint(paint);
        return (int) Math.max(p.measureText(parts[0]), p.measureText(parts[1]));
    }

    private static TextPaint getSuperSubPaint(Paint src)
    {
        TextPaint paint = new TextPaint(src);
        paint.setTextSize(src.getTextSize() / 2.5f);
        return paint;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
    {
        text = text.subSequence(start, end);
        String[] parts = text.toString().split(",");

        Paint p = getSuperSubPaint(paint);

        float width1 = p.measureText(parts[0]);
        float width2 = p.measureText(parts[1]);
        float maxWidth = Math.max(width1, width2);

        canvas.drawText(parts[0], x + (maxWidth - width1), y - (bottom - top) / 3f, p);
        canvas.drawText(parts[1], x + (maxWidth - width2), y + (bottom - top) / 10f, p);
    }
}

然后把它作为:

Then use it as:

Spannable str = new SpannableString("9,4Be -> 2000,127Jo");
str.setSpan(new SuperSubSpan(), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new SuperSubSpan(), 9, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(str);

产生以下结果:

which produces the following result:

一个稍微好一点的解决办法是对这些字符串特殊格式(如{9,4}为 - > {2000,127}乔)并有定期EX pression过程中的字符串,并添加相应的 SuperSubSpans ,只是让你无需手动进行。但实际跨度部将或多或少相同。

A slightly better solution would be to have a special format for these strings (e.g. "{9,4}Be -> {2000,127}Jo") and have a regular expression process the string and add the corresponding SuperSubSpans, just so that you don't need to this manually. But the actual Span part would be more or less the same.

这篇关于如何创建垂直对齐标和下标在TextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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