如何在相同的部分文本设置一个TextView的文本多个跨距?跨距、文本、多个、部分

2023-09-05 06:42:53 作者:过期关系

假设我有下一个文本:

您好计算器

和我想要设置的第二个字既RelativeSizeSpan (设置相对字体尺寸)和TextAppearanceSpan (设置文本的颜色),我该如何合并他们两个?

And I wish to set the second word to be both RelativeSizeSpan (to set a relative font size) and TextAppearanceSpan (to set the color of the text) , how do I merge them both ?

我所知道的是,我可以选择其中的一种,使用下一个code,例如:

All I know is that I can choose one of them , using the next code for example :

final SpannableString textToShow = new SpannableString("Hello stackOverflow");
textToShow.setSpan(new RelativeSizeSpan(1.5f), textToShow.length() - "stackOverflow".length(),textToShow.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(textToShow);

但我还需要设置颜色,甚至是从其他跨类添加其他功能...

But I need to also set the color , or even add other features from other spanning classes ...

我该怎么办?

推荐答案

只需设置额外的跨度。他们将重合/ neccessary时合并。这code对我的作品:

Simply set additional spans. They are going to overlap/merge when neccessary. This code works for me:

final SpannableString text = new SpannableString("Hello stackOverflow");
text.setSpan(new RelativeSizeSpan(1.5f), text.length() - "stackOverflow".length(), text.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), 3, text.length() - 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);