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

如何在Android Studio中使用Fontello图像字体TTF文件?

在Android Studio中使用Fontello图标字体减小应用体积的实现步骤

1. 生成精简的Fontello字体包

  • 访问Fontello平台,只挑选项目实际需要的图标(这是减小体积的核心,避免冗余图标占用空间)。
  • 确认图标选完后,点击下载按钮获取压缩包。

2. 导入字体文件到项目

  • 解压压缩包,找到fontello.ttf文件。
  • 在项目res目录下新建font文件夹,将fontello.ttf拷贝进去。
  • 在res/values下创建fonts.xml,添加字体声明:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/fontello" />
</font-family>

3. 配置图标Unicode映射

  • 从解压包的fontello.css或config.json中,获取每个图标的Unicode编码(比如icon-home对应\e800)。
  • 在res/values/strings.xml中添加图标字符串资源:
<string name="icon_home">\uE800</string>

4. 在项目中使用图标

布局文件中直接使用

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/icon_home"
    android:fontFamily="@font/fontello"
    android:textSize="24sp"
    android:textColor="@color/black" />

代码中动态设置

val iconView = findViewById<TextView>(R.id.icon_view)
iconView.typeface = ResourcesCompat.getFont(this, R.font.fontello)
iconView.text = getString(R.string.icon_home)

5. 进一步优化体积的技巧

  • 严格筛选图标:只保留项目用到的,Fontello支持实时预览和删除多余图标,别打包无用资源。
  • 启用字体压缩:在Module级别的build.gradle中配置:
android {
    buildTypes {
        release {
            aaptOptions {
                additionalParameters "--collapse-resource-names"
            }
        }
    }
}
  • 关闭不必要的PNG压缩:如果项目已全面切换为字体图标,可在release构建中关闭PNG crunching,减少冗余处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 16:43:20