如何为你的APP设置字体

286 阅读1分钟

1. 准备字体文件

首先,需要去google fonts找寻你想要的字体

Material Symbols and Icons - Google Fonts

当然,你也可以使用别的网站中的字体文件,这里就使用google fonts举例了

选择你想要的字体

image.png

点击进去,点击Download

image.png

解压,得到一个.ttf文件

image.png

2. 导入Android Studio中并使用

在res文件夹下新建font文件夹,将刚刚解压得到的.ttf文件导入

image.png

2.1 在xml文件中设置字体

如果你需要直接在xml文件中设置字体的话,则设置TextView控件的fontFamily为你想要的字体即可

image.png

2.2 使用代码进行设置

  • Kotlin语言
val typeface = resources.getFont(R.font.myfont)
textView.typeface = typeface
  • JAVA语言
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);

3. 参考文档

Add a font as an XML resource  |  Android Developers