Hi3751 V352 android9.0 添加并设置默认系统字库

本文详细描述了如何在Android系统中添加特定的TTF文件作为默认字体,涉及Typeface类的加载机制、`fonts.xml`文件的作用以及调试过程中可能遇到的问题。通过修改`fonts.xml`和将TTF文件推送到system/fonts目录,展示了字体配置和测试的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,需求描述

系统中添加某个指定的TTF文件作为系统默认的字库;

源码路径:

frameworks\base\graphics\java\android\graphics\Typeface.java
frameworks\base\data\fonts\fonts.mk
frameworks\base\data\fonts\Android.mk
frameworks\base\data\fonts\fonts.xml

二,实现步骤

  1. Typeface 这个类会在 Android 应用程序启动的过程中,通过反射的方式被加载;打开Typeface.java,可以看到里面有一个static代码块;
    static {
        final ArrayMap<String, Typeface> systemFontMap = new ArrayMap<>();
        final ArrayMap<String, FontFamily[]> systemFallbackMap = new ArrayMap<>();
        buildSystemFallback("/system/etc/fonts.xml", "/system/fonts/", systemFontMap,
                systemFallbackMap);
        sSystemFontMap = Collections.unmodifiableMap(systemFontMap);
        sSystemFallbackMap = Collections.unmodifiableMap(systemFallbackMap);
 
        setDefault(sSystemFontMap.get(DEFAULT_FAMILY));
 
        // Set up defaults and typefaces exposed in public API
        DEFAULT         = create((String) null, 0);
        DEFAULT_BOLD    = create((String) null, Typeface.BOLD);
        SANS_SERIF      = create("sans-serif", 0);
        SERIF           = create("serif", 0);
        MONOSPACE       = create("monospace", 0);
 
        sDefaults = new Typeface[] {
            DEFAULT,
            DEFAULT_BOLD,
            create((String) null, Typeface.ITALIC),
            create((String) null, Typeface.BOLD_ITALIC),
        };
 
    }
  1. fonts.xml文件
    <!-- first font is default -->
    <family name="sans-serif">
        <font weight="100" style="normal">Roboto-Thin.ttf</font>
        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
        <font weight="300" style="normal">Roboto-Light.ttf</font>
        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
        <font weight="400" style="normal">Roboto-Regular.ttf</font>
        <font weight="400" style="italic">Roboto-Italic.ttf</font>
        <font weight="500" style="normal">Roboto-Medium.ttf</font>
        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
        <font weight="900" style="normal">Roboto-Black.ttf</font>
        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
        <font weight="700" style="normal">Roboto-Bold.ttf</font>
        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
    </family>

weight 表示字体粗细的程度;
style表示字体风格,一般来说是常规的和斜体的两种;

  1. 不考虑以上内容,可参考如下修改:
diff --git a/data/fonts/fonts.xml b/data/fonts/fonts.xml
--- a/data/fonts/fonts.xml
+++ b/data/fonts/fonts.xml
@@ -22,18 +22,7 @@
 <familyset version="23">
     <!-- first font is default -->
     <family name="sans-serif">
-        <font weight="100" style="normal">Roboto-Thin.ttf</font>
-        <font weight="100" style="italic">Roboto-ThinItalic.ttf</font>
-        <font weight="300" style="normal">Roboto-Light.ttf</font>
-        <font weight="300" style="italic">Roboto-LightItalic.ttf</font>
-        <font weight="400" style="normal">Roboto-Regular.ttf</font>
-        <font weight="400" style="italic">Roboto-Italic.ttf</font>
-        <font weight="500" style="normal">Roboto-Medium.ttf</font>
-        <font weight="500" style="italic">Roboto-MediumItalic.ttf</font>
-        <font weight="900" style="normal">Roboto-Black.ttf</font>
-        <font weight="900" style="italic">Roboto-BlackItalic.ttf</font>
-        <font weight="700" style="normal">Roboto-Bold.ttf</font>
-        <font weight="700" style="italic">Roboto-BoldItalic.ttf</font>
+        <font weight="400" style="normal">Fonts.ttf</font>
     </family>
 
     <!-- Note that aliases must come after the fonts they reference. -->
  1. 把fonts放置到system/fonts/
diff --git a/data/fonts/Android.mk b/data/fonts/Android.mk
old mode 100644
new mode 100755
index 76eb4e67..6be1723d
--- a/data/fonts/Android.mk
+++ b/data/fonts/Android.mk
@@ -74,7 +74,8 @@ $(eval include $(BUILD_PREBUILT))
 endef
 
 font_src_files := \
-    AndroidClock.ttf
+    AndroidClock.ttf \
+    Fonts.ttf
 
 $(foreach f, $(font_src_files), $(call build-one-font-module, $(f)))
 
diff --git a/data/fonts/fonts.mk b/data/fonts/fonts.mk
old mode 100644
new mode 100755
index e884f2fe..c48f848b
--- a/data/fonts/fonts.mk
+++ b/data/fonts/fonts.mk
@@ -17,4 +17,5 @@
 PRODUCT_PACKAGES := \
     DroidSansMono.ttf \
     AndroidClock.ttf \
+    Fonts.ttf \
     fonts.xml

三,调试方法

  1. 复制fonts.ttf 到system/fonts/目录下,并修改fonts.xml;
adb root
adb remount
adb push 1.ttf system/fonts/Fonts.ttf 
adb shell
chmod 0644 system/fonts/Fonts.ttf
busybox vi system/etc/fonts.xml
  1. 权限不够可能会导致重启之后无法启动系统;
    无法启动的日志如下:
12-31 18:00:35.799 E/Typeface( 2108): Error mapping font file /system/fonts/Fonts.ttf
12-31 18:00:35.800 E/Typeface( 2108): Unable to load Family: sans-serif : null
12-31 18:00:35.925 E/Typeface( 2108): Unable to load Family: sans-serif : nul
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值