简体   繁体   中英

Android: Set text style to part of TextView text

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:

Year :2012, Language :Armenian, Subtitles :English

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"));

EDIT :

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.

textView.setText(Html.fromHtml(
    "<b> Year </b>2012<br> <b> Language </b>"));

为此,您可以使用HTML格式的文本,如下所示。

  textView.setText(Html.fromHtml("<b>Year:</b>2012,<b>Language</b>"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM