[英]How do I add bold to a custom font family then use it for strings?
I read on Stack Overflow how to create a custom bold font and a regular one:我在 Stack Overflow 上阅读了如何创建自定义粗体字体和常规字体:
How to create custom font family with bold font 如何使用粗体创建自定义字体系列
But I'm struggling to understand how to implement this in my strings.但我很难理解如何在我的字符串中实现这一点。 Ordinarily you simply use
<b></b>
to make text bold, but this doesn't seem to work with my custom fonts.通常,您只需使用
<b></b>
将文本加粗,但这似乎不适用于我的自定义字体。 All the text remains the same.所有的文字都保持不变。
I know that you have tried adding the bolded font to a font family, but it hasn't worked.我知道您曾尝试将粗体字体添加到字体系列中,但没有奏效。 You can try one of the ways below.
您可以尝试以下方法之一。 It will work even if you do import a TTF font.
即使您确实导入了 TTF 字体,它也会起作用。
One way you can do it is in strings.xml You can create a new string and then add the b to bold it like this:一种方法是在 strings.xml 中创建一个新字符串,然后添加 b 以将其加粗,如下所示:
<string name="my_string"><b> My String</b></string>
You will need to create one for each string that you want to bold.您需要为要加粗的每个字符串创建一个。 Then in your layout file, you can set the
android:text
to one of the strings from strings.xml and you can set the android:fontFamily
to the TTF font.然后在您的布局文件中,您可以将
android:text
设置为 strings.xml 中的字符串之一,并且可以将android:fontFamily
设置为 TTF 字体。
You can also bold it programmatically using the SpannableString in Java.您还可以使用 Java 中的 SpannableString 以编程方式将其加粗。 Check this article to see how to bold programmatically.
查看本文以了解如何以编程方式加粗。 Hope it helps!
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.