-1
我已经在我的应用程序中放入了一个片段内的textview。每个页面都有不同的文字。我希望每当你找到例如“树”这个词时,这个词就被涂成红色,其余的词则变成白色。我怎么能这样做?感谢在TextView中为文本设置不同的颜色Android
我已经在我的应用程序中放入了一个片段内的textview。每个页面都有不同的文字。我希望每当你找到例如“树”这个词时,这个词就被涂成红色,其余的词则变成白色。我怎么能这样做?感谢在TextView中为文本设置不同的颜色Android
Pattern pattern = Pattern.compile("tree");
Matcher matcher = pattern.matcher(yourTextViewText);
final SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(yourTextViewText);
final ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
while (matcher.find()) {
spannableBuilder.setSpan(
span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
yourTextView.setText(spannableBuilder);
这将用RED颜色代替所有“树”字。
要更改的文字,使用的颜色:
textview.setTextColor(Color.RED);
非常感谢。我的问题解决了。 – user3270441
并在你的代码中添加大胆的加法? – user3270441