首页 > 解决方案 > 如何在不使用 xml 的 Java 类中格式化 Java 字符串

问题描述

我设置了一个字符串数组列表来将文本填充到多个介绍屏幕。由于文本填充在实际的 Java 类中,而不是在 xml 资源文件中,我想知道如何在 java 类中使文本中的某些单词变为粗体?

 public String [] slide_descriptions = {
        "I want this text in bold \n",
        "and maybe this too!"
};

标签: javaandroid

解决方案


您可以使用 Html 标签。例如:

textView.setText(Html.fromHtml("< b >This is Bold< /b >"));

对于字符串,这样做:

 public String [] slide_descriptions = {
        "<b>I want this text in bold</b> \n",
        "<b>and maybe this too!</b>"
};

所以最后当你把字符串放在文本视图中时:

textView.setText(Html.fromHtml(slide_description));

推荐阅读