简体   繁体   English

如何将粗体添加到自定义字体系列然后将其用于字符串?

[英]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.

相关问题 如何使用粗体创建自定义字体系列 - How to create custom font family with bold font 如何在自定义字体系列中获得粗体样式? - How to get bold style in custom font family? 如何在 font_family.xml 中使用属性 android:fontStyle=&quot;bold&quot; 设置自定义字体 - How to set custom font in font_family.xml with attribute android:fontStyle=“bold” 当您的手机使用自定义字体系列时,如何为应用设置字体系列? - How to set a font family to an app when your phone use custom font family? 如何在RecyclerView中使用的字符串上使用自定义字体? - How do you use a custom font on Strings that are being used in a RecyclerView? 如何添加自动响应粗体的 Adroid Studio 自定义字体? - how add in Adroid Studio custom font, which reacts on bold automatically? 如何使用字体系列中的特定字体 - How to use a specific font from a font family Flutter:未应用自定义字体系列 - Flutter: Custom font family do not get applied Android将Roboto字体设置为粗体,斜体,常规,…(类似于自定义字体系列) - Android set Roboto font with bold, italic, regular,… (something like custom font family) 如何在Android中的菜单项上添加自定义字体? - How do i add custom font on menu item in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM