可下载字体的字体粗细不起作用 android

Downloadable Font's font weight not working android

我正在关注 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"
/>

如您所见,我已经尝试了几种方法,但 none 还没有成功。字体颜色和字号根据我定义的样式变化但是加粗效果没有注册

这是我得到的输出:

预期输出:

我的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>

我不确定这是否是正确的解决方法,但我设法通过在我的 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>

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

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

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>

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

我 运行 同样的事情——我不确定 textFontWeight 是否能正确使用可下载的字体。也许它不会提示应用程序实际下载该重量的字体?

您可以通过为每个要使用的粗细创建一个字体资源来修复它。尝试添加此字体资源:

poppins_bold.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="name=Poppins&amp;weight=700"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

您可能还需要向 res/layout/values/preloaded_fonts.xml 添加一个条目:

<item>@font/poppins_bold</item>

然后用android:fontFamily="@font/poppins_bold引用它。

FWIW,如果您通过 selecting the weight in the Layout Editor 添加字体,也会产生相同的更改,但我发现复制字体资源并编辑 fontProviderQuery.

更容易