在 Android 中使用 Html.fromHtml() 突出显示文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Highlighting Text Color using Html.fromHtml() in Android?
提问by sunil
I am developing an application in which there will be a search screen where user can search for specific keywords and that keyword should be highlighted. I have found Html.fromHtml method.
我正在开发一个应用程序,其中将有一个搜索屏幕,用户可以在其中搜索特定关键字,并且应突出显示该关键字。我找到了 Html.fromHtml 方法。
But I will like to know whether its the proper way of doing it or not.
但我想知道这是否是正确的做法。
Please let me know your views on this.
请让我知道您对此的看法。
回答by Christopher Orr
Or far simpler than dealing with Spannable
s manually, since you didn't say that you want the background highlighted, just the text:
或者比Spannable
手动处理s简单得多,因为您没有说要突出显示背景,只是文本:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
回答by SergeyA
Using color value from xml resource:
使用来自 xml 资源的颜色值:
int labelColor = getResources().getColor(R.color.label_color);
String сolorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!!
Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE);
回答by stealthcopter
This can be achieved using a Spannable String. You will need to import the following
这可以使用 Spannable String 来实现。您将需要导入以下内容
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.text.style.StyleSpan;
And then you can change the background of the text using something like the following:
然后您可以使用以下内容更改文本的背景:
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);
Where this will highlight the charecters at pos 1 - 4 with a red color. Hope this helps!
这将用红色突出显示位置 1 - 4 处的字符。希望这可以帮助!
回答by Khyati Fatania
String name = modelOrderList.get(position).getName(); //get name from List
String text = "<font color='#000000'>" + name + "</font>"; //set Black color of name
/* check API version, according to version call method of Html class */
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
Log.d(TAG, "onBindViewHolder: if");
holder.textViewName.setText(context.getString(R.string._5687982) + " ");
holder.textViewName.append(Html.fromHtml(text));
} else {
Log.d(TAG, "onBindViewHolder: else");
holder.textViewName.setText("123456" + " "); //set text
holder.textViewName.append(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY)); //append text into textView
}
回答by Vidar Vestnes
Alternative solution: Using a WebView instead. Html is easy to work with.
替代解决方案:改用 WebView。Html 易于使用。
WebView webview = new WebView(this);
String summary = "<html><body>Sorry, <span style=\"background: red;\">Madonna</span> gave no results</body></html>";
webview.loadData(summary, "text/html", "utf-8");
回答by mhdtouban
font is deprecated use span instead Html.fromHtml("<span style=color:red>"+content+"</span>")
字体已弃用,请改用 span Html.fromHtml("<span style=color:red>"+content+"</span>")
回答by bsma
To make part of your text underlined and colored
使部分文本带有下划线和彩色
in your strings.xml
在你的 strings.xml 中
<string name="text_with_colored_underline">put the text here and <u><font color="#your_hexa_color">the underlined colored part here<font><u></string>
then in the activity
然后在活动中
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_colored_underline)));
and for clickable links:
以及可点击的链接:
<string name="text_with_link"><![CDATA[<p>text before link<a href=\"http://www.google.com\">title of link</a>.<p>]]></string>
and in your activity:
并在您的活动中:
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_link)));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
回答by Ch Khurram
First Convert your string into HTML then convert it into spannable .
首先将您的字符串转换为 HTML,然后将其转换为 spannable 。
Spannable spannable = new SpannableString(Html.fromHtml(labelText));
Spannable spannable = new SpannableString(Html.fromHtml(labelText));
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), spannable.toString().indexOf("?"), spannable.toString().lastIndexOf("?") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), spannable.toString().indexOf("?"), spannable.toString().lastIndexOf("?") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
回答by Naina
textview.setText(Html.fromHtml("<font color='rgb'>"+text contain+"</font>"));
It will give the color exactly what you have made in html editor , just set the textview and concat it with the textview value. Android does not support span color, change it to font color in editor and you are all set to go.
它将提供您在 html 编辑器中所做的颜色,只需设置 textview 并将其与 textview 值连接即可。Android 不支持跨度颜色,在编辑器中将其更改为字体颜色即可。