如何在android中为字符串的某些部分着色?

时间:2014-10-03 09:38:48

标签: android

我已经实施了搜索列表 它从给定列表视图中的模式输出匹配的字符串。 现在我想只为我的字符串中与模式匹配的部分着色。 请帮我。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

你正在寻找spannable: http://developer.android.com/reference/android/text/Spannable.html

示例:

SpannableString demotext = new SpannableString("All your base are belong to me");
// Make 'All' (chars 0 to 3) red
demotext.setSpan(new ForegroundColorSpan(Color.RED), 0, 3, 0);
((TextView) getView().findViewById(R.id.fragment_disclaimer_title)).setText(demotext);

http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring http://eazyprogramming.blogspot.co.uk/2013/06/spannable-string-in-android-url-span.html

答案 1 :(得分:0)

为你的字符串的一部分颜色你可以这样做:

String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);

修改后。