我正在开发一个应用程序,其中将有一个搜索屏幕,在那里用户可以搜索特定的关键字,该关键字应突出显示。我找到了Html.fromHtml方法。
但我想知道这是否是正确的方法。
请告诉我你对此的看法。
发布于 2010-05-30 18:14:01
或者比手动处理Spannable
简单得多,因为您没有说要突出显示背景,只希望突出显示文本:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
发布于 2013-02-21 02:04:07
使用xml资源中的颜色值:
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);
发布于 2010-04-29 01:19:08
这可以使用Spannable字符串来实现。您将需要导入以下内容
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
然后,您可以使用类似以下内容来更改文本的背景:
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处的字符。希望这能有所帮助!
https://stackoverflow.com/questions/2730706
复制相似问题