如何在 XML 中为字体使用指定的权重 [英] How to use specified weights for fonts in XML

查看:17
本文介绍了如何在 XML 中为字体使用指定的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 XML 中的字体 功能,您可以为字体系列指定各种字体粗细.例如:

Using the Fonts in XML feature you can specify various font weights for a font family. For example:

<?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/archivo_narrow_regular" android:fontWeight="400" android:fontStyle="normal"
        app:font="@font/archivo_narrow_regular" app:fontWeight="400" app:fontStyle="normal"/>

    <font android:font="@font/archivo_narrow_regular_italic" android:fontWeight="400" android:fontStyle="italic"
        app:font="@font/archivo_narrow_regular_italic" app:fontWeight="400" app:fontStyle="italic"/>

    <font android:font="@font/archivo_narrow_medium" android:fontWeight="500" android:fontStyle="normal"
        app:font="@font/archivo_narrow_medium" app:fontWeight="500" app:fontStyle="normal"/>

    <font android:font="@font/archivo_narrow_medium_italic" android:fontWeight="500" android:fontStyle="italic"
        app:font="@font/archivo_narrow_medium_italic" app:fontWeight="500" app:fontStyle="italic"/>

    <font android:font="@font/archivo_narrow_semibold" android:fontWeight="600" android:fontStyle="normal"
        app:font="@font/archivo_narrow_semibold" app:fontWeight="600" app:fontStyle="normal"/>

    <font android:font="@font/archivo_narrow_semibold_italic" android:fontWeight="600" android:fontStyle="italic"
        app:font="@font/archivo_narrow_semibold_italic" app:fontWeight="600" app:fontStyle="italic"/>

    <font android:font="@font/archivo_narrow_bold" android:fontWeight="700" android:fontStyle="normal"
        app:font="@font/archivo_narrow_bold" app:fontWeight="700" app:fontStyle="normal"/>

    <font android:font="@font/archivo_narrow_bold_italic" android:fontWeight="700" android:fontStyle="italic"
        app:font="@font/archivo_narrow_bold_italic" app:fontWeight="700" app:fontStyle="italic"/>

</font-family>

但我不知道如何实际使用这些权重;可以在 XML(布局/样式)文件中,也可以在 Java 代码中.它们没有可用于 TextViewfontWeight 属性,以及 TypefaceResourcesCompat.getFont(context, R.font.archivo_narrow) 没有提到字体粗细.

But I cannot figure out how to actually make use of each of these weights; either in an XML (layout/style) file, or in Java code. Their is no fontWeight attribute available for TextView, and the Typeface object created from ResourcesCompat.getFont(context, R.font.archivo_narrow) has no mention of font weights.

我意识到我可以只指定特定的字体资源(即 R.font.archivo_narrow_semibold),但是在font-family?

I realize that I can just specify the specific font resource (i.e. R.font.archivo_narrow_semibold), but then what is the point of having a fontWeight attribute in the font-family?

一个新的静态create(Typeface family, int weight, boolean italic) 方法已在 API 级别 28 中添加,以及 getWeight() 实例方法.这最终使得在 Java 代码中使用 fontWeight 属性成为可能;虽然仅适用于 API 级别 28 及以上,但我在支持库中没有找到任何类似物.

A new static create(Typeface family, int weight, boolean italic) method was added in API Level 28, along with a getWeight() instance method. This finally makes it possible to make use of the fontWeight attribute in Java code; though only for API Level 28 and above, I haven't found any analogs in the support library.

这很有用——并且表明 fontWeight 属性在过去没有任何用途——但我真的希望能够在 XML 样式中使用权重.

This is useful—and shows that the fontWeight attribute didn't serve any purpose in the past—but I would really like to be able to use the weight in XML styling.

推荐答案

正如@FlorianWalther 所指出的,TextView.textFontWeight 属性正是我想要的.这个属性显然是在 API 级别 28 中添加的,但是 直到最近才被记录.

As pointed out by @FlorianWalther, the TextView.textFontWeight attribute is exactly what I was looking for. This attribute was apparently added in API level 28, but was not documented until recently.

确保所有权重都在同一个 XML 文件中(如我的问题),然后只需将该属性与 fontFamily 属性一起使用.

Make sure all of the weights are in the same XML file (as in my question), then simply use the attribute along with the fontFamily attribute.

<TextView android:id="@+id/weightedTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/archivo_narrow"
    android:textFontWeight="700"/>

据我所知,此属性仅在 API 级别 28 及以上可用,如果我在支持库中找到它的对应项,我会更新.

As far as I am aware this attribute is only available in API Level 28 an above, I will update if I find its counterpart in the support library.

这篇关于如何在 XML 中为字体使用指定的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆