首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Android中使用Html.fromHtml()突出显示文本颜色?

在Android中使用Html.fromHtml()突出显示文本颜色?
EN

Stack Overflow用户
提问于 2010-04-28 23:09:34
回答 9查看 115.2K关注 0票数 87

我正在开发一个应用程序,其中将有一个搜索屏幕,在那里用户可以搜索特定的关键字,该关键字应突出显示。我找到了Html.fromHtml方法。

但我想知道这是否是正确的方法。

请告诉我你对此的看法。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2010-05-30 18:14:01

或者比手动处理Spannable简单得多,因为您没有说要突出显示背景,只希望突出显示文本:

代码语言:javascript
复制
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
票数 208
EN

Stack Overflow用户

发布于 2013-02-21 02:04:07

使用xml资源中的颜色值:

代码语言:javascript
复制
int labelColor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2); // alpha value

Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE); 
票数 38
EN

Stack Overflow用户

发布于 2010-04-29 01:19:08

这可以使用Spannable字符串来实现。您将需要导入以下内容

代码语言:javascript
复制
import android.text.SpannableString; 
import android.text.style.BackgroundColorSpan; 
import android.text.style.StyleSpan;

然后,您可以使用类似以下内容来更改文本的背景:

代码语言:javascript
复制
TextView text = (TextView) findViewById(R.id.text_login);
text.setText("");
text.append("Add all your funky text in here");
Spannable sText = (Spannable) text.getText();
sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);

这将用红色突出位置1-4处的字符。希望这能有所帮助!

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2730706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档