android - 如何在我的应用程序中使用多种字体样式?
问题描述
在我的项目中,我想为我的应用程序使用 Monsterrat 字体,在某些屏幕上,我想使用 Monsterrat 斜体,Monsterat 中号。我想声明一次并在整个应用程序中使用它们。我不明白我该怎么做。
解决方案
第一种方式:
将您的字体放入 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 的答案并创建两个自定义文本视图,每个都有自己的样式、大小、颜色等。调用自定义每个文本视图,这样您就可以在同一布局中拥有两种样式(但这不是一个好习惯除了某些类型的布局)
推荐阅读
- tensorflow - Tensorflow LSTM 排序作为诗歌示例,但使用 3 个浮点数而不是字符串
- typescript - 突变标识函数的隐式或显式返回
- pandas - 熊猫重写函数而不调用apply
- java - 反序列化抽象父类型的构造函数参数时获取 UnresolvedForwardReference
- javascript - Vue:冲突的顺序。添加了以下模块
- python - 带 args 参数的 Python 求和
- sql - 保留 SQLite 中表达式之前的子字符串
- linux - 每秒将“perf stat”输出保存到文件中
- python - sklearn Logistic回归中的C参数是什么?
- django - ElasticSearch、FarmHaystack、Django 连接被拒绝