Multiple RelativeSizeSpan on same line

醉酒当歌 提交于 2019-12-05 18:36:19

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

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);

Check this code this is giving the proper output as per your requirement

editText=(TextView)findViewById(R.id.editText);
        editText.setTextSize(40);
        SpannableString span1 = new SpannableString("25%");
        SpannableString span2 = new SpannableString("25%");

        span1.setSpan(new RelativeSizeSpan(0.3f),  2, 3, 0);
        span1.setSpan(new RelativeSizeSpan(1.5f),  0, 2, 0);

        span2.setSpan(new RelativeSizeSpan(0.3f),  2, 3, 0);
        span2.setSpan(new RelativeSizeSpan(1.5f),  0, 2, 0);

        editText.setText(TextUtils.concat(span1," " ,span2));

firstSpanEnd has a value of 3 in your example.

The first RelativeSizeSpan would then be applied to the characters [0, 2] -> 25.

The second RelativeSizeSpan would be applied to the characters [3, 4] which is the \n not the % character.

ssb.setSpan(new RelativeSizeSpan(1.5f), 0, firstSpanEnd-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ssb.setSpan(new RelativeSizeSpan(0.3f), firstSpanEnd-1, firstSpanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

should fix the issue but I guess there's an underlying layout bug that causes the space (the \n probably) showing before the %.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!