首页 > 解决方案 > 如何在我的应用程序中使用多种字体样式?

问题描述

在我的项目中,我想为我的应用程序使用 Monsterrat 字体,在某些屏幕上,我想使用 Monsterrat 斜体,Monsterat 中号。我想声明一次并在整个应用程序中使用它们。我不明白我该怎么做。

标签: androidfontsandroid-fonts

解决方案


第一种方式:

将您的字体放入 res/font 目录并创建两种样式。在此示例中,我还设置了文本颜色和文本大小:

<style name="FirstStyle" parent="Theme.AppCompat.Light.NoActionBar" >
    <item name="android:fontFamily">@font/md_grotesk_regular</item>
    <item name="fontFamily">@font/md_grotesk_regular</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textSize">@dimen/font_12</item>
</style>

然后在 Manifest 中为每个活动声明 android:theme,如下所示:

<activity
    android:name=".SomeActivity"
    android:theme="@style/FirstStyle" />

这将为活动中定义的所有文本设置所选样式。

第二种方式:

查看@Sandeep Parish 的答案并创建两个自定义文本视图,每个都有自己的样式、大小、颜色等。调用自定义每个文本视图,这样您就可以在同一布局中拥有两种样式(但这不是一个好习惯除了某些类型的布局)


推荐阅读