同一行上有多个RelativeSizeSpan [英] Multiple RelativeSizeSpan on same line

查看:682
本文介绍了同一行上有多个RelativeSizeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试彼此相邻使用两个RelativeSizeSpan,但是两者之间出现了很大的差距.看看我想要实现的目标和得到的成就.

I'm trying to use two RelativeSizeSpan next to each other, but a big gap appears in between. See what I want to achieve and what I'm getting.

这就是我构建Spanned实例的方式

This is how I'm building the Spanned instance

    String formattedValue = "25%";
    SpannableStringBuilder ssb = new SpannableStringBuilder(formattedValue);
    ssb.append("\n");
    ssb.append(otherValue);

    int firstSpanEnd = formattedValue.length();
    ssb.setSpan(new RelativeSizeSpan(1.5f), 0, firstSpanEnd-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ssb.setSpan(new RelativeSizeSpan(0.3f), firstSpanEnd, firstSpanEnd+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

这就是我绘制的方式

  float maxTextWidth = getPaintCenterText().measureText(mText, 0, mText.length());

  TextPaint textPaint = new TextPaint(getPaintCenterText());

  mCenterTextLayout = new StaticLayout(mText, textPaint, (int) maxTextWidth, Layout.Alignment.ALIGN_NORMAL, 0.85f, 0, false);

  c.save();

  float centerY = center.y - totalheight;

  c.translate(center.x, centerY);

  mCenterTextLayout.draw(c);

  c.restore();

如果我删除第二个RelativeSizeSpan,间隙会变小,但仍然存在.

If I remove the second RelativeSizeSpan the gap is smaller, but it's still there.

此外,我尝试了多个Spanned标志,但似乎没有任何作用.

Also, I've tried several Spanned flags, but none seem to have any effect.

我的SpannableStringBuilder的mText字段

mText field of my SpannableStringBuilder

[0] = '2' 48
[1] = '5' 46
[2] = '%' 48
[3] = '\n' 10
[4] = 'E' 69
[5] = 'x' 120
[6] = 'e' 101
[7] = 'r' 114
[8] = 'c' 99
[9] = 'i' 105
[10] = 's' 115
[11] = 'e' 101
[12] = ' ' 32
[13] = 'd' 100
[14] = 'a' 97
[15] = 'y' 121
[16] = 's' 115
[17] = '\u0000' 0
[18] = '\u0000' 0
[19] = '\u0000' 0
[20] = '\u0000' 0
[21] = '\u0000' 0
[22] = '\u0000' 0

推荐答案

TextPaint上设置setTextAlign(Paint.Align.CENTER)可能会干扰您的跨度.

Setting setTextAlign(Paint.Align.CENTER) on TextPaint might be interfering with your spans.

相反,请在StaticLayout上使用Layout.Alignment.CENTER,它将整个文本居中.

Instead, use Layout.Alignment.CENTER on the StaticLayout which should center the text as a whole.

mCenterTextLayout = new StaticLayout(mText, textPaint, (int) maxTextWidth, 
                          Layout.Alignment.ALIGN_CENTER, 0.85f, 0, false);

这篇关于同一行上有多个RelativeSizeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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