Android自定义字体赢得';除非以编程方式完成,否则不能使用粗体

Android自定义字体赢得';除非以编程方式完成,否则不能使用粗体,android,fonts,android-xml,Android,Fonts,Android Xml,正如标题所示,我有常规、粗体等自定义字体。当我在xml中将字体设置为粗体或斜体时,它将显示在设计中,但不会出现在实际设备上(三星galaxy s9)。我尝试了几种不同的策略,唯一能让它正常工作的方法就是通过编程设置字体 Typeface boldFont = ResourcesCompat.getFont(getContext(), R.font.cogito_bold); testTextView.setTypeface(boldFont); 下面是textview的xml布局 <Te

正如标题所示,我有常规、粗体等自定义字体。当我在xml中将字体设置为粗体或斜体时,它将显示在设计中,但不会出现在实际设备上(三星galaxy s9)。我尝试了几种不同的策略,唯一能让它正常工作的方法就是通过编程设置字体

Typeface boldFont = ResourcesCompat.getFont(getContext(), R.font.cogito_bold);
testTextView.setTypeface(boldFont);
下面是textview的xml布局

<TextView
    android:id="@+id/testText"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_weight="4"
    android:autoSizeMaxTextSize="44sp"
    android:autoSizeMinTextSize="20sp"
    android:autoSizeStepGranularity="2sp"
    android:autoSizeTextType="uniform"
    android:textSize="42sp"
    android:fontFamily="@font/cogito_font"
    android:textStyle="bold"
    android:text="Test Words"
    android:textColor="@color/wb_white" />

这是我的自定义字体系列,然后在同一res/font目录下有.otf文件。我还尝试过直接将textview的fontfamily设置为cogito_bold.otf,但没有成功

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

    <font
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:font="@font/cogito_italic"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/cogito_italic"
        app:fontStyle="italic"
        app:fontWeight="400" />

    <font
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:font="@font/cogito_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/cogito_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
</font-family>

无需创建字体系列即可完成此操作。 只需将字体添加到res>font目录

app\u font\u bold.ttf

app\u font\u regular.ttf

并调用XML中的字体

android:fontFamily="@font/app_font_bold"

您可以在不创建字体族的情况下执行此操作。 只需将字体添加到res>font目录

app\u font\u bold.ttf

app\u font\u regular.ttf

并调用XML中的字体

android:fontFamily="@font/app_font_bold"

可能是因为您的字体不包括在google fontsgoogle字体系列中,您可以通过以下途径找到可用的字体:;设计>单击您的文本视图>属性选项卡>字体系列>单击下拉菜单>更多字体…(在底部)可能是因为您的字体未包含在google fonts中。google fonts fonts系列可通过以下途径找到:;设计>单击您的文本视图>属性选项卡>字体系列>单击下拉菜单>更多字体…(在底部)我创建了字体系列,只需将样式添加到其中,就可以轻松加粗。我认为问题在于我扩展的基本活动导致了字体问题。将其更改为ExtendeAppCompatActivity似乎解决了这个问题。我创建了字体系列,只需在其中添加样式,就可以轻松地进行加粗。我认为问题在于我扩展的基本活动导致了字体问题。将其更改为扩展AppCompatActivity似乎解决了这个问题。