我不知道如何使TextView上的特定文本变成粗体。
是这样的
txtResult.setText(id+" "+name);
我希望输出是这样的:
1111年尼尔
id和名称是我从数据库中检索值的变量,我想将id改为粗体,但只有id,所以名称不会受到影响,我不知道如何做到这一点。
我不知道如何使TextView上的特定文本变成粗体。
是这样的
txtResult.setText(id+" "+name);
我希望输出是这样的:
1111年尼尔
id和名称是我从数据库中检索值的变量,我想将id改为粗体,但只有id,所以名称不会受到影响,我不知道如何做到这一点。
当前回答
这是我使用的Kotlin扩展函数
/**
* Sets the specified Typeface Style on the first instance of the specified substring(s)
* @param one or more [Pair] of [String] and [Typeface] style (e.g. BOLD, ITALIC, etc.)
*/
fun TextView.setSubstringTypeface(vararg textsToStyle: Pair<String, Int>) {
val spannableString = SpannableString(this.text)
for (textToStyle in textsToStyle) {
val startIndex = this.text.toString().indexOf(textToStyle.first)
val endIndex = startIndex + textToStyle.first.length
if (startIndex >= 0) {
spannableString.setSpan(
StyleSpan(textToStyle.second),
startIndex,
endIndex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
this.setText(spannableString, TextView.BufferType.SPANNABLE)
}
用法:
text_view.text="something bold"
text_view.setSubstringTypeface(
Pair(
"something bold",
Typeface.BOLD
)
)
.
text_view.text="something bold something italic"
text_view.setSubstringTypeface(
Pair(
"something bold ",
Typeface.BOLD
),
Pair(
"something italic",
Typeface.ITALIC
)
)
其他回答
以防有人使用数据绑定。我们可以像这样定义绑定适配器
@BindingAdapter("html")
fun setHtml(view: TextView, html: String) {
view.setText(HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY))
}
然后我们可以在TextView上使用它
app:html="@{@string/bold_text}"
bold_text在哪里
<string name="bold_text"><![CDATA[Part of text is <b>bold</b>]]></string>
找到了一种方法,如果你想处理多种语言的本地化,这很无聊,但它是有效的,让我们假设我们想要这样:
英文:
没有付款记录
西班牙语:
没有付款记录
你必须创建3个字符串
中文:
<string name="start_string">There are no</string>
<string name="middle_string">payments</string>
<string name="end_string">registered.</string>
<string name="string_format" translatable="false">%1$s %2$s %3$s</string>
西班牙语:
<string name="start_string">No hay</string>
<string name="middle_string">pagos</string>
<string name="end_string">registrados</string>
现在你可以这样做:
val startSpanPosition = getString(R.string.start_string).length
val endSpanPosition = startSpanPosition + getString(R.string.middle_string).length
val mySpannableString = SpannableStringBuilder(String.format(getString(R.string.string_format),
getString(R.string.start_string), getString(R.string.middle_string))), getString(R.string.end_string)))
mySpannableString.setSpan(StyleSpan(Typeface.BOLD), spanStartPosition, endSpanPosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
字符串资源
<resources>
<string name="your_string_resource_name">This is normal text<![CDATA[<b> but this is bold </b>]]> and <![CDATA[<u> but this is underline text</u>]]></string>
</resources>
您的Java类
yourtextView.setText(getString(R.string.your_string_resource_name));
这不是最简单的解决方案吗?
<string name="string">Please provide your <b>Name</b> properly</string>
只要使用你想要的字符串:)
结果如下所示:
请填写您的姓名
首先:你不需要担心从Raghav Sood的答案使用缓慢的性能代码。
第二:使用Kotlin时,不需要编写w3bshark的答案提供的扩展函数。
最后:所有你需要做的就是从谷歌使用Kotlin android-ktx库(参考这里找到更多信息以及如何将它包含在你的项目中):
// Suppose id = 1111 and name = neil (just what you want).
val s = SpannableStringBuilder()
.bold { append(id) }
.append(name)
txtResult.setText(s)
产量:1111尼尔
更新:
因为我认为它可以帮助其他人,也可以展示你可以走多远,这里有更多的用例。
当你需要用蓝色和斜体显示文本时: val myCustomizedString = SpannableStringBuilder() .color(blueColor, {append(“蓝色文本”)}) .append("显示") .italic{append(“这是无痛的”)} 当你需要同时以粗体和斜体显示文本时: 粗体{斜体{追加(“粗体和斜体”)}}
简而言之,粗体、追加、颜色和斜体是SpannableStringBuilder的扩展函数。您可以在官方文档中看到其他扩展函数,从那里您可以考虑其他可能性。