我的简书:https://www.jianshu.com/u/c91e642c4d90
我的CSDN:http://blog.csdn.net/wo_ha
我的GitHub:https://github.com/chuanqiLjp
我的个人博客:https://chuanqiljp.github.io/
版权声明:转载需要在醒目注明出处
序言
上一篇Android 自定义字体,更换系统默认显示的字体使用自定义字体有讲到怎样指定控件显示指定字体,怎样整个软件显示指定字体,怎样WebView加载指定字体,但是还留下一个怎样修改整个系统的默认字体,由于内容较多,所以单独抽离出来讲,由于要操作系统文件,因此需要Root权限或系统签名,自己在操作前建议先备份下字体配置文件/system/etc/system_fonts.xml和/system/etc/fallback_fonts.xml,否则操作失败有可能开机后无法进入桌面,此时就需要将备份的system_fonts.xml推送到对应目录下并修改为对应的权限。
版权声明:本文可被转载,但需要在醒目位置注明原文出处:https://www.jianshu.com/p/b0b541b94427
1、字体加载原理
- ① Android 系统的字体文件:位于 /system/fonts/ 文件夹下,我们可以到对应的目录下进行查看,可以看出,Android的字体文件都是ttf文件,命令顺序:adb shell ——>cd /system/fonts/ ——>ll,查看结果如下图所示:
- ② 在/system/etc/目录下有两个字体配置文件,分别是system_fonts.xml 和 fallback_fonts.xml ,当系统需要加载字体时,会优先从 system_fonts.xml 文件开始查找,如果没有找到再进入 fallback_fonts.xml 查找。
system_fonts.xml示范文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87<?xml version="1.0" encoding="UTF-8"?><!-- System Fonts This file lists the font families that will be used by default for all supported glyphs. Each entry consists of a family, various names that are supported by that family, and up to four font files. The font files are listed in the order of the styles which they support: regular, bold, italic and bold-italic. If less than four styles are listed, then the styles with no associated font file will be supported by the other font files listed. The first family is also the default font, which handles font request that have not specified specific font names. Any glyph that is not handled by the system fonts will cause a search of the fallback fonts. The default fallback fonts are specified in the file /system/etc/fallback_fonts.xml, and there is an optional file which may be supplied by vendors to specify other fallback fonts to use in /vendor/etc/fallback_fonts.xml.-->
<familyset>
<family>
<nameset>
<name>sans-serif</name>
<name>arial</name>
<name>helvetica</name>
<name>tahoma</name>
<name>verdana</name>
</nameset>
<fileset>
<file>Roboto-Regular.ttf</file>
<file>Roboto-Bold.ttf</file>
<file>Roboto-Italic.ttf</file>
<file>Roboto-BoldItalic.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>sans-serif-light</name>
</nameset>
<fileset>
<file>Roboto-Light.ttf</file>
<file>Roboto-LightItalic.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>sans-serif-thin</name>
</nameset>
<fileset>
<file>Roboto-Thin.ttf</file>
<file>Roboto-ThinItalic.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>sans-serif-condensed</name>
</nameset>
<fileset>
<file>RobotoCondensed-Regular.ttf</file>
<file>RobotoCondensed-Bold.ttf</file>
<file>RobotoCondensed-Italic.ttf</file>
<file>RobotoCondensed-BoldItalic.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>serif</name>
<name>times</name>
<name>times new roman</name>
<name>palatino</name>
<name>georgia</name>
<name>baskerville</name>
<name>goudy</name>
<name>fantasy</name>
<name>cursive</name>
<name>ITC Stone Serif</name>
</nameset>
<fileset>
<file>DroidSerif-Regular.ttf</file>
<file>DroidSerif-Bold.ttf</file>
<file>DroidSerif-Italic.ttf</file>
<file>DroidSerif-BoldItalic.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>Droid Sans</name>
</nameset>
<fileset>
<file>DroidSans.ttf</file>
<file>DroidSans-Bold.ttf</file>
</fileset>
</family>
<family>
<nameset>
<name>monospace</name>
<name>courier</name>
<name>courier new</name>
<name>monaco</name>
</nameset>
<fileset>
<file>DroidSansMono.ttf</file>
</fileset>
</family>
</familyset>
fallback_fonts.xml 示范文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178<?xml version="1.0" encoding="utf-8"?><!-- Fallback Fonts This file specifies the fonts, and the priority order, that will be searched for any glyphs not handled by the default fonts specified in /system/etc/system_fonts.xml. Each entry consists of a family tag and a list of files (file names) which support that family. The fonts for each family are listed in the order of the styles that they handle (the order is: regular, bold, italic, and bold-italic). The order in which the families are listed in this file represents the order in which these fallback fonts will be searched for glyphs that are not supported by the default system fonts (which are found in /system/etc/system_fonts.xml). Note that there is not nameset for fallback fonts, unlike the fonts specified in system_fonts.xml. The ability to support specific names in fallback fonts may be supported in the future. For now, the lack of files entries here is an indicator to the system that these are fallback fonts, instead of default named system fonts. There is another optional file in /vendor/etc/fallback_fonts.xml. That file can be used to provide references to other font families that should be used in addition to the default fallback fonts. That file can also specify the order in which the fallback fonts should be searched, to ensure that a vendor-provided font will be used before another fallback font which happens to handle the same glyph. Han languages (Chinese, Japanese, and Korean) share a common range of unicode characters; their ordering in the fallback or vendor files gives priority to the first in the list. Language-specific ordering can be configured by adding a BCP 47-style "lang" attribute to a "file" element; fonts matching the language of text being drawn will be prioritised over all others.-->
<familyset>
<family>
<fileset>
<file variant="elegant">DroidNaskh-Regular.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">DroidNaskhUI-Regular.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>DroidSansEthiopic-Regular.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>DroidSansHebrew-Regular.ttf</file>
<file>DroidSansHebrew-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansThai-Regular.ttf</file>
<file variant="elegant">NotoSansThai-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansThaiUI-Regular.ttf</file>
<file variant="compact">NotoSansThaiUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>DroidSansArmenian.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>DroidSansGeorgian.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansDevanagari-Regular.ttf</file>
<file variant="elegant">NotoSansDevanagari-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansDevanagariUI-Regular.ttf</file>
<file variant="compact">NotoSansDevanagariUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansTamil-Regular.ttf</file>
<file variant="elegant">NotoSansTamil-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansTamilUI-Regular.ttf</file>
<file variant="compact">NotoSansTamilUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansMalayalam-Regular.ttf</file>
<file variant="elegant">NotoSansMalayalam-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansMalayalamUI-Regular.ttf</file>
<file variant="compact">NotoSansMalayalamUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansBengali-Regular.ttf</file>
<file variant="elegant">NotoSansBengali-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansBengaliUI-Regular.ttf</file>
<file variant="compact">NotoSansBengaliUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansTelugu-Regular.ttf</file>
<file variant="elegant">NotoSansTelugu-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansTeluguUI-Regular.ttf</file>
<file variant="compact">NotoSansTeluguUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansKannada-Regular.ttf</file>
<file variant="elegant">NotoSansKannada-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansKannadaUI-Regular.ttf</file>
<file variant="compact">NotoSansKannadaUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansKhmer-Regular.ttf</file>
<file variant="elegant">NotoSansKhmer-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansKhmerUI-Regular.ttf</file>
<file variant="compact">NotoSansKhmerUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="elegant">NotoSansLao-Regular.ttf</file>
<file variant="elegant">NotoSansLao-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file variant="compact">NotoSansLaoUI-Regular.ttf</file>
<file variant="compact">NotoSansLaoUI-Bold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>NanumGothic.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>Padauk-book.ttf</file>
<file>Padauk-bookbold.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>NotoSansSymbols-Regular.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>AndroidEmoji.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>NotoColorEmoji.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file>DroidSansFallback.ttf</file>
</fileset>
</family>
<family>
<fileset>
<file lang="ja">MTLmr3m.ttf</file>
</fileset>
</family> <!-- Note: complex scripts (i.e. those requiring shaping in Harfbuzz) have a cumulative limit of 64k glyphs. Thus, if they are placed after the large fonts such as DroidSansFallback, they are likely to render incorrectly. Please use caution when putting fonts toward the end of the list. -->
</familyset>
2、为系统添加一个默认字体
修改系统默认字体的原理:根据系统字体加载原理可知,我们只需要在路径 /system/fonts/ 下添加我们自定义的ttf字体文件,然后修改 /system/etc/system_fonts.xml 字体配置文件,按照响应的格式添加一个节点,由于需要系统默认使用该字体,因此该节点需要是根节点familyset下的第一个子节点,系统在system_fonts.xml中找到了该字体的配置,故不会去fallback_fonts.xml 寻找,因此也只需要修改这一个配置文件即可,文件修改成功后需要注意已修改文件的读写权限(否则会没有效果),为了方便,我们设置全部用户可读可写。
① 判断系统字体库是否已有需要添加的字体文件
1 | /** |
② 若①判断后没有则需要拷贝字体文件到对应路径下
1 | /** |
③ 判断字体配置文件 system_fonts.xml 是否已有该节点,若有则不进行操作,若无进行步骤④
1 | /** |
④ 若③中没有该节点,则在根节点familyset下的第一位置添加该节点并保存
1 | /** |
⑤ 完整的调用流程
1 | private String system_fonts = "/system/etc/system_fonts.xml"; |
⑥ 运行Root指令的方法
1 | /** |
3、删除已添加的系统默认字体
和添加字体相对应,需要先删除字体文件,然后再删除 system_fonts.xml和fallback_fonts.xml两文件中的对应节点,由于我们没有修改过fallback_fonts.xml文件因此不需要做删除操作
① 删除节点是需要调用checkXmlNode方法坚持节点是否存在,若存在则删除数据
1 | /** |
② 完整的删除流程
1 | public void deleteYwsflsjtForSystem(View view) { |
我的CSDN博客:http://blog.csdn.net/wo_ha/article/details/79202632