Android:将文本样式设置为 TextView 文本的一部分 [英] Android: Set text style to part of TextView text

查看:33
本文介绍了Android:将文本样式设置为 TextView 文本的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中,很容易为整个 TextView 设置样式:

In Android, it is easy to set a style for an entire TextView:

textView.setTypeface(null, Typeface.BOLD_ITALIC);
textView.setTypeface(null, Typeface.BOLD);
textView.setTypeface(null, Typeface.ITALIC);

有没有办法为视图中的特定单词(或字符)设置样式?我想生成如下一行:

Is there a way to set a style for specific words (or characters) within the view? I would like to generate a line like:

年份:2012,语言:亚美尼亚语,字幕:英语

Year:2012, Language:Armenian, Subtitles:English

推荐答案

是的,您可以使用 Html.fromHtml 将特定单词的样式更改为:

Yes, you can use Html.fromHtml to change style of specific word as:

textview.setText(Html.fromHtml("<b>" + Year: + "</b>"+"2012,
                  "+"<b>" + Language: + "</b>"+":Armenian,"
                  "+"<b>" + Subtitles: + "</b>"+":English"));

如果您在使用 Html.fromHtml(...) 时遇到任何问题,那么您可以使用 SpannableString 用于创建具有不同字体、颜色或样式的字符串.

if you have any issue to use Html.fromHtml(...) then you can go for SpannableString for Creating an String with different fonts,colors or style.

这篇关于Android:将文本样式设置为 TextView 文本的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆