如何在相同的部分文本设置一个TextView的文本多个跨距? [英] How to set multiple spans on a TextView's text on the same partial text?
问题描述
假设我有下一个文本:
您好计算器
和我想要设置的第二个字既<一个href="http://developer.android.com/reference/android/text/style/RelativeSizeSpan.html">RelativeSizeSpan (设置相对字体尺寸)和<一href="http://developer.android.com/reference/android/text/style/TextAppearanceSpan.html">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);
这篇关于如何在相同的部分文本设置一个TextView的文本多个跨距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!