【问题标题】:Downloadable Font's font weight not working android可下载字体的字体粗细不起作用android
【发布时间】:2020-08-25 07:28:29
【问题描述】:

我关注this official guide 并想在我的项目中使用 Poppins 字体。字体已更改,但字体粗细属性似乎不起作用。

我的风格定义如下:

<style name="TextAppearance.SectionTitle" parent="TextAppearance.MaterialComponents.Headline4">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#223263</item>
    <item name="android:fontWeight">700</item>
    <item name="fontWeight">700</item>
</style>

我将其应用如下:

<TextView
    ...
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/category"
    android:textStyle="bold"
    android:textFontWeight="700"
    style="@style/TextAppearance.SectionTitle"
/>

如您所见,我已经尝试了多种方法,但都没有成功。字体颜色和大小根据我定义的样式变化,但没有注册粗体效果。

这是我得到的输出:

预期输出:

我的 poppins.xml:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
    app:fontProviderAuthority="com.google.android.gms.fonts"
    app:fontProviderPackage="com.google.android.gms"
    app:fontProviderQuery="Poppins"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

preloaded_fonts.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <array name="preloaded_fonts" translatable="false">
    <item>@font/poppins</item>
    <item>@font/poppins_bold</item>
  </array>
</resources>

【问题讨论】:

    标签: android kotlin android-downloadable-fonts


    【解决方案1】:

    我不确定这是否是正确的解决方法,但我设法通过将字体粗细替换为 style 标记中的字体系列来解决它,如下所示:

    <style name="TextAppearance.SectionTitle" parent="TextAppearance.MaterialComponents.Headline4">
        ...
        <item name="android:fontFamily">@font/poppins_bold</item>
        <item name="fontFamily">@font/poppins_bold</item>
    </style>
    

    为此,您需要在资源中添加所选字体的粗体:

    【讨论】:

    • Android 是 Android。与我尝试过的任何其他选项不同,这很有效。它也不能在 textAppearance 样式下使用,只能在顶级样式本身下使用。 :|
    【解决方案2】:

    你试过this answer吗?我们已经使用 Inter Google Font 实现了它,它运行良好,显示了正确的权重。

    res/font/inter.xml

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <font
            android:font="@font/inter_thin"
            android:fontStyle="normal"
            android:fontWeight="100"
            app:font="@font/inter_thin"
            app:fontStyle="normal"
            app:fontWeight="100" />
    
        <font
            android:font="@font/inter_extralight"
            android:fontStyle="normal"
            android:fontWeight="200"
            app:font="@font/inter_extralight"
            app:fontStyle="normal"
            app:fontWeight="200" />
    
        <font
            android:font="@font/inter_light"
            android:fontStyle="normal"
            android:fontWeight="300"
            app:font="@font/inter_light"
            app:fontStyle="normal"
            app:fontWeight="300" />
    
        <font
            android:font="@font/inter_regular"
            android:fontStyle="normal"
            android:fontWeight="400"
            app:font="@font/inter_regular"
            app:fontStyle="normal"
            app:fontWeight="400" />
    
        <font
            android:font="@font/inter_medium"
            android:fontStyle="normal"
            android:fontWeight="500"
            app:font="@font/inter_medium"
            app:fontStyle="normal"
            app:fontWeight="500" />
    
        <font
            android:font="@font/inter_semibold"
            android:fontStyle="normal"
            android:fontWeight="600"
            app:font="@font/inter_semibold"
            app:fontStyle="normal"
            app:fontWeight="600" />
    
        <font
            android:font="@font/inter_bold"
            android:fontStyle="normal"
            android:fontWeight="700"
            app:font="@font/inter_bold"
            app:fontStyle="normal"
            app:fontWeight="700" />
    
        <font
            android:font="@font/inter_extrabold"
            android:fontStyle="normal"
            android:fontWeight="800"
            app:font="@font/inter_extrabold"
            app:fontStyle="normal"
            app:fontWeight="800" />
    
        <font
            android:font="@font/inter_black"
            android:fontStyle="normal"
            android:fontWeight="900"
            app:font="@font/inter_black"
            app:fontStyle="normal"
            app:fontWeight="900" />
    
    </font-family>
    
    

    Inter字体的所有ttf文件也放在res/font中。

    这是我们如何将其应用于 TextView 样式的示例:

    res/values/styles.xml

        <style name="TextAppearance.Medium">
            <item name="android:fontFamily">@font/inter</item>
        </style>
    

        <style name="Whatever">
            <item name="android:layout_width">wrap_content</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:fontFamily">@font/inter</item>
        </style>
    

    我希望这些示例对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 2020-11-07
      • 2015-09-24
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多