在 Java Android 中使字符串文本加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24110930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:12:17  来源:igfitidea点击:

Make a String text Bold in Java Android

javaandroidstringtext

提问by meskh

I want to make Habit Number: bold , I tried the HTML tag but it didn't work. I did some research but couldn't find any. Hope someone is able to help me. Thanks!

我想让Habit Number: bold ,我尝试了 HTML 标签,但没有用。我做了一些研究,但找不到任何。希望有人能够帮助我。谢谢!

String habitnumber = "Habit Number: " + String.valueOf(habitcounter);
String Title = habit.getTitle();
String description = habit.getDescription();


//Set text for the row
                tv.setText(habitnumber+ "\n" + Title + " \n" + description + "\n --------------------");

采纳答案by Rod_Algonquin

I tried the HTML tag but it didn't work

I think that you didnt wrap your string to Html.fromHtml

我认为你没有把你的绳子包起来 Html.fromHtml

solution:

解决方案:

You can do it with HTML using html tag for bold: <b>

您可以使用 html 标记为粗体使用 HTML 来做到这一点: <b>

 String habitnumber = "<b>" + "Habit Number: " + "</b> " + String.valueOf(habitcounter);
 tv.setText(Html.fromHtml(habitnumber ));

回答by JaKoZo

tv.setTypeface(null, Typeface.BOLD);

回答by Ronny Kibet

Like Gabe mentioned, try using the use Html.fromHtml or use spannable. Check this out: Selecting, Highlighting, or Styling Portions of Text

就像 Gabe 提到的那样,尝试使用 use Html.fromHtml 或使用 spannable。看看这个:选择、突出显示或设置文本的样式部分

You could basically use html tags in your string resource like:

您基本上可以在字符串资源中使用 html 标签,例如:

<resource>
<string id="@+id/styled_welcome_message"><b>your text</b></string>
</resources>

回答by antorqs

Use this in the XML file, inside the textview

在文本视图中的 XML 文件中使用它

android:textStyle="bold"