Android 如何为XML中的字体使用指定的权重

Android 如何为XML中的字体使用指定的权重,android,android-xml,android-styles,android-fonts,Android,Android Xml,Android Styles,Android Fonts,使用该功能,可以为字体族指定各种字体权重。例如: <?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"&

使用该功能,可以为字体族指定各种字体权重。例如:

<?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代码中。它们没有可用于
TextView
fontWeight
属性,并且从中创建的对象没有提及字体权重

我意识到我可以只指定特定的字体资源(即
R.font.archivo\u slown\u semibold
),但是在
字体系列中使用
fontwweight
属性有什么意义呢


更新 API级别28中添加了一个新的静态方法以及一个实例方法。这最终使得在Java代码中使用
fontWeight
属性成为可能;虽然只针对API级别28及以上,但我在支持库中没有找到任何类似的


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

它看起来像android遵循android应用程序字体管理和大小调整的web标准

“字体重量”属性用于定义字体的重量,如普通字体或粗体字体

但对于所有其他权重,使用的数值范围为100到900。web字体面临的一个挑战是,大多数web浏览器不支持正常和粗体以外的字体权重。下表描述了权重到数值定义的可能映射:

100    Extra Light or Ultra Light
200    Light or Thin
300    Book or Demi
400    Normal or Regular
500    Medium
600    Semibold, Demibold
700    Bold
800    Black, Extra Bold or Heavy
900    Extra Black, Fat, Poster or Ultra Black
您可以阅读有关字体重量的更多信息


cc_montserrat_bold.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/montserrat_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/montserrat_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
    <font
        android:font="@font/montserrat_bolditalic"
        android:fontStyle="italic"
        android:fontWeight="700"
        app:font="@font/montserrat_bolditalic"
        app:fontStyle="italic"
        app:fontWeight="700" />

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


</font-family>

cc_montserrat_regular.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/montserrat_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/montserrat_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
    <font
        android:font="@font/montserrat_bolditalic"
        android:fontStyle="italic"
        android:fontWeight="700"
        app:font="@font/montserrat_bolditalic"
        app:fontStyle="italic"
        app:fontWeight="700" />

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


</font-family>

Kotlin用法:

val textView = dialog.findViewById<TextView>(android.R.id.message) as TextView
val typeface = ResourcesCompat.getFont(context,R.font.cc_montserrat_regular)
        textView.typeface = typeface
val textView=dialog.findviewbyd(android.R.id.message)作为textView
val typeface=ResourcesCompat.getFont(context,R.font.cc\u montserrat\u regular)
textView.typeface=字体
Android项目截图:

val textView = dialog.findViewById<TextView>(android.R.id.message) as TextView
val typeface = ResourcesCompat.getFont(context,R.font.cc_montserrat_regular)
        textView.typeface = typeface

编辑:这不是请求的解决方案:/

我找到了Typeface.Builder类,我想这正是您想要的。不幸的是,它只能从API级别26及更高版本获得。也许compat库很快就会出现


下面是如何在android studio中使用不同字体的权重

  • 打开
    app/src/main/res/layou\u awesome\u layout.xml
  • 选择
    设计选项卡
  • 在组件树面板中,打开您的
    TextView
    ,然后选择右菜单底部的
    查看所有属性
  • 在属性面板中,打开
    fontFamily
    下拉列表并选择
    More Fonts…
  • 选择家庭
    YouFontFamilyName
  • 选择样式
    Regular/Bold/Semibold/Black/WhateverStyleYouHaveThere

  • 瞧,我认为这是实现你想要的唯一方法。

    正如@FlorianWalther所指出的,属性正是我所寻找的。该属性显然是在API级别28中添加的,但是

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

    <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 an以上版本中可用,如果在支持库中找到对应的属性,我将进行更新。

    步骤1:查找或下载您的字体,比如cavier_dream

    步骤2:右键单击res文件夹并在res文件夹中创建字体资源文件夹

    步骤3:右键单击字体文件夹并创建一个字体资源文件,比如app_font.xml

    步骤4:打开app_font.xml文件,并进行如下修改

    <?xml version="1.0" encoding="utf-8"?>
    
    
    

    
    

    用法:

    val textView = dialog.findViewById<TextView>(android.R.id.message) as TextView
    val typeface = ResourcesCompat.getFont(context,R.font.cc_montserrat_regular)
            textView.typeface = typeface
    
    要在整个应用程序中全局使用,请转到styles.xml,并在主题内部创建一个新项,如下所示

     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:fontFamily">@font/app_font</item>
    </style>
    
    
    @颜色/原色
    @颜色/原色暗
    @颜色/颜色重音
    @字体/应用程序字体
    
    对于本地使用,只需使用fontFamily属性并将应用程序字体设置为值


    快乐编码:)

    由于
    textFontWeight
    属性是API级别28及更高,支持旧设备的一个解决方法是为每个字体重量创建一个单独的fontFamily,并从视图中引用fontFamily

    res/layout/your_layout.xml

    <TextView
        ...
        android:fontFamily="@font/fontFamily_your_font_demibold 
    />
    

    一种字体有几种不同的权重:粗体、半粗体、中等、普通。
    对我来说,当我为每个权重创建一个.xml文件时,它终于起作用了,使用normal和italic,然后在布局或样式中使用
    android:fontFamily:
    fontFamily:
    引用它们。后者很重要,否则它只能在非常高的API级别下工作

        <font-family xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
    
        <font android:font="@font/internal_inter_bold" 
            android:fontStyle="normal"
            android:fontWeight="700"
            app:font="@font/internal_inter_bold"
            app:fontStyle="normal"
            app:fontWeight="700" />
        <font android:font="@font/internal_inter_bold_italic"
            android:fontStyle="italic"
            android:fontWeight="700"
            app:font="@font/internal_inter_bold_italic"
            app:fontStyle="italic"
            app:fontWeight="700" />
         </font-family>
    
    
    
         <style name="numbers_large">
            <item name="android:textSize">32sp</item>
            <item name="android:fontFamily">@font/inter_bold</item>
            <item name="fontFamily">@font/inter_bold</item>
        </style>
    
    
    32便士
    @字体/中间粗体
    @字体/中间粗体
    
    即使在安卓5.0中,它也能工作,并没有对较低版本进行测试


    因此,基本上为每个字体重量创建一个font.xml文件,同时使用app:和android:前缀,然后使用这两个前缀引用它们。

    请参阅官方的android参考以了解字体重量值:

    android:fontWeight

    整数。字体的重量。当字体加载到字体堆栈中并覆盖字体标题表中的任何权重信息时,将使用此属性属性值必须是正数,是100的倍数,介于100和900之间,包括100和900。如果不指定属性,应用程序将使用字体标题表中的值。最常见的值是400表示常规重量,700表示粗体重量


    Florian在关于<style name="TextAppearance.MdcTypographyStyles.Headline1" parent="TextAppearance.MaterialComponents.Headline1"> <item name="fontFamily">@font/my_font_weight_light</item> <item name="android:fontFamily">@font/my_font_weight_light</item> <item name="android:textSize">123sp</item> <item name="android:letterSpacing">-0.0122</item> </style>
    <font-family xmlns:app="http://schemas.android.com/apk/res-auto">
        <font
            app:font="@font/my_font_light_italic"
            app:fontStyle="italic"
            app:fontWeight="400" />
        <font
            app:font="@font/my_font_light"
            app:fontStyle="normal"
            app:fontWeight="400" />
    </font-family>
    
    style="@style/TextAppearance.MdcTypographyStyles.Headline1"