【发布时间】:2011-02-13 09:56:48
【问题描述】:
我正在开发一个应用程序,其中会有一个搜索屏幕 用户可以在哪里搜索特定的关键字,并且该关键字应该是 突出显示。我找到了 Html.fromHtml 方法。
但我想知道这是正确的做法还是 不是。
请让我知道您对此的看法。
【问题讨论】:
标签: android text highlight textview
我正在开发一个应用程序,其中会有一个搜索屏幕 用户可以在哪里搜索特定的关键字,并且该关键字应该是 突出显示。我找到了 Html.fromHtml 方法。
但我想知道这是正确的做法还是 不是。
请让我知道您对此的看法。
【问题讨论】:
标签: android text highlight textview
或者比手动处理Spannables 简单得多,因为您没有说要突出显示背景,只是要突出显示文本:
String styledText = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
【讨论】:
<span style="color:#ff0000;">,这样 android 就不会更改颜色。
使用来自 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);
【讨论】:
这可以使用 Spannable String 来实现。您将需要导入以下内容
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 处的字符。希望这会有所帮助!
【讨论】:
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
}
【讨论】:
替代解决方案:改用 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");
【讨论】:
字体已弃用,使用 span 而不是 Html.fromHtml("<span style=color:red>"+content+"</span>")
【讨论】:
使部分文本加下划线和着色
在您的 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>
然后在活动中
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_colored_underline)));
对于可点击的链接:
<string name="text_with_link"><![CDATA[<p>text before link<a href=\"http://www.google.com\">title of link</a>.<p>]]></string>
在你的活动中:
yourTextView.setText(Html.fromHtml(getString(R.string.text_with_link)));
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
【讨论】:
textview.setText(Html.fromHtml("<font color='rgb'>"+text contain+"</font>"));
它将完全按照您在 html 编辑器中制作的颜色给出,只需设置 textview 并将其与 textview 值连接即可。 Android 不支持跨度颜色,在编辑器中将其更改为字体颜色即可。
【讨论】:
首先将您的字符串转换为 HTML,然后将其转换为 spannable。按照以下代码的建议进行操作。
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);
【讨论】:
同时添加 Kotlin 版本:
strings.xml)colors.xml)fun getMulticolorSpanned(): Spanned {
// Get text from resources
val text: String = getString(R.string.your_text_from_resources)
// Get color from resources and parse it to HEX (RGB) value
val warningHexColor = getHexFromColors(R.color.your_error_color)
// Use above string & color in HTML
val html = "<string>$text<span style=\"color:#$warningHexColor;\">*</span></string>"
// Parse HTML (base on API version)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(html)
}
}
和 Kotlin 扩展(移除 alpha):
fun Context.getHexFromColors(
colorRes: Int
): String {
val labelColor: Int = ContextCompat.getColor(this, colorRes)
return String.format("%X", labelColor).substring(2)
}
【讨论】: