简体   繁体   English

如何在 font_family.xml 中使用属性 android:fontStyle="bold" 设置自定义字体

[英]How to set custom font in font_family.xml with attribute android:fontStyle=“bold”

I want to add custom bold font in my font_family.xml .我想在我的font_family.xml添加自定义粗体字体。 I did not find bold fontStyle="bold" in font family developer doc .我在字体系列开发人员文档中没有找到粗体fontStyle="bold" It only have normal or italic attribute.它只有normalitalic属性。 Here is font_family.xml这是font_family.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

So i have to use style like所以我必须使用像

<style name="fontBoldStyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/montserrat_semi_bold</item>
</style>

or like this或者像这样

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/montserrat_semi_bold"/>

My question我的问题

  1. What does fontStyle attribute do? fontStyle属性有什么作用? Even i did not provide italic font to font_family.xml , and set textStyle="italic" then my font is italic.即使我没有为font_family.xml提供斜体字体,并设置textStyle="italic"然后我的字体是斜体。
  2. how to add custom bold font in font family and set android:fontStyle="bold" in font_family.xml so i dont have to create different styles and i just have to set textStyle="bold"如何在字体系列中添加自定义粗体并在font_family.xml设置android:fontStyle="bold"所以我不必创建不同的样式,我只需要设置textStyle="bold"

So what is a solution that i can set 3 of my fonts - normal, bold, italic to font_family.xml, and set android:fontFamily="@font/font_family" on TextView and then i can use android:textStyle="bold" or italic, or normal?那么什么是我可以设置 3 种字体的解决方案 - normal, bold, italic到 font_family.xml,并在 TextView 上设置android:fontFamily="@font/font_family"然后我可以使用android:textStyle="bold"或斜体,或正常?

For your 2nd question :对于您的第二个问题

You can add a custom bold font in a font-family , by defining a font with fontWeight="700" :您可以通过使用fontWeight="700"定义字体,在font-family添加自定义粗体字体:

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
    <font
        android:fontStyle="normal"
        android:fontWeight="700"
        android:font="@font/montserrat_semi_bold" />
</font-family>

If a TextView uses this font-family with textStyle="bold" , it will show up in montserrat_semi_bold如果TextView使用带有textStyle="bold" font-family ,它将显示在montserrat_semi_bold

There is a way you can do this by applying property called android:textStyle in your layout.xml you can do this:有一种方法可以通过在android:textStyle中应用名为android:textStyle属性来执行此操作,您可以执行以下操作:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold" />

Setting font bold programatically this way以这种方式以编程方式设置字体粗体

mTextView.setTypeface(Typeface.ttf, Typeface.BOLD);

Or或者

mTextView.setTypeface(mTextView.getTypeface(), Typeface.BOLD);

or You can use paint object and set it to text或者您可以使用绘画对象并将其设置为文本

声明:本站的技术帖子网页,遵循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? Android将Roboto字体设置为粗体,斜体,常规,…(类似于自定义字体系列) - Android set Roboto font with bold, italic, regular,… (something like custom font family) 将TextView设置为带有xml属性和自定义字体的粗体字体(不起作用) - Setting TextView to bold font with xml-attribute and custom font (not working) 如何将粗体添加到自定义字体系列然后将其用于字符串? - How do I add bold to a custom font family then use it for strings? 在字体家族xml中将Costum字体添加到android - Adding costum font to android in font family xml android 找不到资源字体系列@font/your_font_xml - android no resource found font family @font/your_font_xml 当您的手机使用自定义字体系列时,如何为应用设置字体系列? - How to set a font family to an app when your phone use custom font family? 如何为整个android应用程序设置相同的字体系列 - How to set same font family to entire android application 如何为整个 Android 应用设置默认字体系列 - How to set default font family for entire Android app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM