Android将Roboto字体设置为粗体、斜体、规则,。。。(类似于自定义字体系列)

Android将Roboto字体设置为粗体、斜体、规则,。。。(类似于自定义字体系列),android,fonts,typeface,font-family,Android,Fonts,Typeface,Font Family,我知道如何在Android应用程序中以编程方式设置自定义字体。 有没有办法加载自定义字体(资产)的字体,Android框架将使用基于粗体、斜体等的适当文件 例如,现在我尝试将Roboto字体设置为某个TextView Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); textView.setTypeface(typeface); 它工作正常。但由于我将xml

我知道如何在Android应用程序中以编程方式设置自定义字体。 有没有办法加载自定义字体(资产)的字体,Android框架将使用基于粗体、斜体等的适当文件

例如,现在我尝试将Roboto字体设置为某个
TextView

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);
它工作正常。但由于我将xml布局内部的
TextView
设置为粗体,所以文本不粗体

<TextView
    android:id="@+id/my_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="50dp"
    android:textStyle="bold"
    android:gravity="center"
    android:text="@string/my_text"
    android:textColor="@color/my_foreground"
    android:textSize="24dp" />
在我的资产目录中只有一个“字体系列”


如何在一个字体/系列中加载所有字体?

我不知道如何将单个字体的不同字体变体视为一个系列,但字体往往具有较大的文件大小,因此您可能无论如何都不想将所有这些字体导入应用程序。相反,您可以只使用中等字体,然后设置粗体和斜体属性

例如,如果在XML布局中设置了android:textStyle=“bold”,则可以在代码中这样做以保持粗体样式:

Typeface currentTypeFace = textView.getTypeface();
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
    textView.setTypeface(tf, Typeface.BOLD);
} else {
    textView.setTypeface(tf);
}

不确定发链接是否是个好主意,但无论如何。。。 这是我关于这个主题的博客,它有一个类可以完全解决这个问题。

看起来不错,但格式已损坏。为什么不在这里粘贴同一个类呢?链接是断开的,对不起,网站已经关闭多年了。您现在可以使用android数据绑定来使用任何类型的字体
Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf
Typeface currentTypeFace = textView.getTypeface();
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
    textView.setTypeface(tf, Typeface.BOLD);
} else {
    textView.setTypeface(tf);
}