You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio开发应用时使用本地自定义字体?

在Android开发中使用本地自定义字体的操作方法

一、修改Android Studio编辑器字体(编写代码时的显示字体)

  • 打开Android Studio,进入 File > Settings(Windows/Linux)或 Android Studio > Settings(Mac)
  • 在左侧导航栏选择 Editor > Font
  • 取消勾选「Use system font」,从「Font」下拉列表中选择你本地已安装的字体
  • 按需调整字号、行高参数,点击 Apply 和 OK 保存设置

二、让开发的Android应用使用本地自定义字体(应用运行时显示)

步骤1:导入字体文件到项目

  • 在 app/src/main 下创建 assets/fonts 目录(若不存在),将本地字体文件(.ttf/.otf 格式)复制进去
  • 或针对Android 8.0+,直接将字体文件放入 res/font 目录(无则创建)

步骤2:应用字体到控件或全局

方法1:代码中动态设置(兼容所有Android版本)

针对单个TextView:

// Kotlin
val textView = findViewById<TextView>(R.id.tv_example)
val customTypeface = Typeface.createFromAsset(assets, "fonts/your_font.ttf")
textView.typeface = customTypeface
// Java
TextView textView = findViewById(R.id.tv_example);
Typeface customTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_font.ttf");
textView.setTypeface(customTypeface);

方法2:XML布局中设置(Android 8.0+)

  • 若字体放在 res/font 目录,可直接在布局中引用:
<TextView
    android:id="@+id/tv_example"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/your_font" />
  • 要设置全局默认字体,修改 res/values/styles.xml:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="android:fontFamily">@font/your_font</item>
</style>

内容的提问来源于stack exchange,提问作者Roham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 10:42:32