字体是从google noto官网下载的字体。这里添加的是otf结尾的字体。
1.拷贝要添加的字体
frameworks/base/data/fonts/
把要添加的字体拷贝到这里。
2.修改Android.mk
把要添加的字体加到这个下面font_src_files
font_src_files := \
AndroidClock.ttf \
NotoSansCJKjp-Black.otf \
NotoSansCJKjp-Bold.otf \
NotoSansCJKjp-DemiLight.otf \
NotoSansCJKjp-Light.otf \
NotoSansCJKjp-Medium.otf \
NotoSansCJKjp-Regular.otf \
NotoSansCJKjp-Thin.otf \
NotoSansMonoCJKjp-Bold.otf \
NotoSansMonoCJKjp-Regular.otf
3.修改fonts.mk
把要添加的字体添加到这个下面PRODUCT_PACKAGES
PRODUCT_PACKAGES := \
DroidSansFallback.ttf \
DroidSansMono.ttf \
AndroidClock.ttf \
NotoSansCJKjp-Black.otf \
NotoSansCJKjp-Bold.otf \
NotoSansCJKjp-DemiLight.otf \
NotoSansCJKjp-Light.otf \
NotoSansCJKjp-Medium.otf \
NotoSansCJKjp-Regular.otf \
NotoSansCJKjp-Thin.otf \
NotoSansMonoCJKjp-Bold.otf \
NotoSansMonoCJKjp-Regular.otf
4.修改fonts.xml
文件中第一个family就是系统的默认字体,添加如下代码。alias name是一个映射。
关于weight可以参考这里http://www.jianshu.com/p/f9c6f9729fbb
<family name="NotoSansCJKjp">
<font weight="100" style="normal">NotoSansCJKjp-Thin.otf</font>
<font weight="200" style="normal">NotoSansCJKjp-Light.otf</font>
<font weight="300" style="normal">NotoSansCJKjp-DemiLight.otf</font>
<font weight="400" style="normal">NotoSansCJKjp-Regular.otf</font>
<font weight="500" style="normal">NotoSansCJKjp-Medium.otf</font>
<font weight="600" style="normal">NotoSansCJKjp-Bold.otf</font>
<font weight="700" style="normal">NotoSansCJKjp-Black.otf</font>
</family>
<alias name="NotoSansCJKjp-Thin" to="NotoSansCJKjp" weight="100" />
<alias name="NotoSansCJKjp-Light" to="NotoSansCJKjp" weight="200" />
<alias name="NotoSansCJKjp-DemiLight" to="NotoSansCJKjp" weight="300" />
<alias name="NotoSansCJKjp-Regular" to="NotoSansCJKjp" weight="400" />
<alias name="NotoSansCJKjp-Medium" to="NotoSansCJKjp" weight="500" />
<alias name="NotoSansCJKjp-Bold" to="NotoSansCJKjp" weight="600" />
<alias name="NotoSansCJKjp-Black" to="NotoSansCJKjp" weight="700" />
<family name="NotoSansMonoCJKjp">
<font weight="600" style="normal">NotoSansMonoCJKjp-Bold.otf</font>
<font weight="700" style="normal">NotoSansMonoCJKjp-Regular.otf</font>
</family>
<alias name="NotoSansMonoCJKjp-Bold" to="NotoSansMonoCJKjp"
weight="600" />
<alias name="NotoSansMonoCJKjp-Regular" to="NotoSansMonoCJKjp"
weight="700" />
然后编译,直接mmm的话会在out/target/product/xxx/system/fonts/下面生产你添加的字体文件。
在out/target/product/xxx/system/etc/下面会更新fonts.xml文件。
但是我测试的时候fonts.xml没更新。所以直接build整个项目。
5.使用字体
NotoSansCJKjp-Black就是刚才那里的映射。
<TextView
android:fontFamily="NotoSansCJKjp-Black"
android:text="誇りとゲーラ国竜カードや管理の経験が早く追いつきましたカード"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/test"
android:layout_alignParentStart="true"
android:layout_marginStart="12dp"
android:layout_marginTop="63dp" />
效果如下:
这是在没有添加字体的手机上运行的效果
这是在添加了字体的手机上运行的效果