本文介绍了自定义的TextView android系统中有不同的颜色的话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能有一个的TextView
有不同颜色的每一个字?甚至每一个字母?我试图延长的TextView
和创造,但不过,我认为这个问题是,我将如何绘制所有的文字出来,同时用不同的颜色?
Is it possible to have a textview
to have different color for every word? Or even every letter? I tried extending textview
and creating it but however I thought of the problem is, how would I draw all the the text out at the same time with different colors?
推荐答案
使用<$c$c>android.text.Spannable$c$c>
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(
new ForegroundColorSpan(Color.BLUE),
wordStart,
wordEnd,
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
);
myTextView.setText(str);
修改:为了让所有的Java绿色
EDIT: To make all "Java" green
final Pattern p = Pattern.compile("Java");
final Matcher matcher = p.matcher(text);
final SpannableStringBuilder spannable = new SpannableStringBuilder(text);
final ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
while (matcher.find()) {
spannable.setSpan(
span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
myTextView.setText(spannable);
这篇关于自定义的TextView android系统中有不同的颜色的话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!