Android添加系统字体fonts

本文详细介绍了如何在Android系统中预置新字体,包括创建fonts_customization.xml文件、添加ttf文件以及在编译mk中引用。通过修改Android.mk和配置文件,开发者可以为设备添加自定义字体供应用使用。
摘要由CSDN通过智能技术生成


一、product下预置新字体

系统源生提供了定制化增加字体文件的方式,可以在不修改源生代码的情况下进行定制增加。

product下的预置参考了google提供的预置方式。
Adding Custom Fonts | Android Open Source Project
模板代码文件为custom-fonts.zip文件

方案的主要工作是:

  • 将定制的fonts_customization.xml配置文件编译到系统中。
  • 将自定义的字体ttf文件编译到系统中。

在这里插入图片描述

1.预置fonts_customization.xml文件

mydevice_overlay/fonts/Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := fonts_customization.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_PREBUILT_MODULE_FILE := $(LOCAL_PATH)/fonts_customization.xml
LOCAL_PRODUCT_MODULE := true

include $(BUILD_PREBUILT)

mydevice_overlay/fonts/fonts.mk

PRODUCT_PACKAGES := \
    fonts_customization.xml \

$(call inherit-product-if-exists, vendor/XXX/fonts/oem-lobster/fonts.mk)

mydevice_overlay/fonts/fonts_customization.xml
这个文件是增加了一个名为lobster的字体,使用的字体文件为Lobster-Regular.ttf

<fonts-modification version="1">
        <family customizationType="new-named-family" name="lobster">
                <font weight="400" style="normal">Lobster-Regular.ttf</font>
        </family>
</fonts-modification>

2.预置ttf文件

/fonts/oem-lobster/fonts.mk

PRODUCT_PACKAGES := \
    Lobster-Regular.ttf \

/fonts/oem-lobster/Android.mk

LOCAL_PATH := $(call my-dir)

# Build the rest of font files as prebuilt.
# $(1): The source file name in LOCAL_PATH.
#       It also serves as the module name and the dest file name.
define build-one-font-module
$(eval include $(CLEAR_VARS))\
$(eval LOCAL_MODULE := $(1))\
$(eval LOCAL_SRC_FILES := $(1))\
$(eval LOCAL_MODULE_CLASS := ETC)\
$(eval LOCAL_MODULE_TAGS := optional)\
$(eval LOCAL_MODULE_PATH := $(TARGET_OUT_PRODUCT)/fonts)\
$(eval LOCAL_PRODUCT_MODULE := true) \
$(eval include $(BUILD_PREBUILT))
endef

font_src_files := \
    Lobster-Regular.ttf \

$(foreach f, $(font_src_files), $(call build-one-font-module, $(f)))

build-one-font-module :=
font_src_files :=

上传/fonts/oem-lobster/Lobster-Regular.ttf文件

3.在系统的编译mk中添加对此新增mk的引用

例如/device/common/common.mk

$(call inherit-product-if-exists, vendor/XXX/fonts/mydevice_overlay/fonts/fonts.mk)

二、字体使用

之后,在应用中便能够使用lobster字体了

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"
        android:fontFamily="lobster"/>
  • 13
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要获取 Android 系统中的字体列表,可以使用以下代码: ```java public class FontListActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_font_list); ListView listView = findViewById(R.id.font_list_view); final List<String> fontList = new ArrayList<>(); // 获取系统字体列表 String[] fontNames = getResources().getStringArray(R.array.system_fonts); // 将字体添加到列表中 Collections.addAll(fontList, fontNames); // 设置列表适配器 ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fontList); listView.setAdapter(adapter); } } ``` 其中,`R.array.system_fonts` 是一个存储系统字体名称的字符串数组,需要在 `strings.xml` 文件中进行定义。例如: ```xml <string-array name="system_fonts"> <item>Default</item> <item>Droid Sans</item> <item>Droid Serif</item> <item>Droid Sans Mono</item> <item>Roboto</item> <item>Roboto Condensed</item> <item>Roboto Light</item> <item>Roboto Medium</item> <item>Roboto Thin</item> <item>Roboto Black</item> </string-array> ``` 当然,如果你需要获取系统所有字体的详细信息,可以使用 `Typeface` 类中的 `getFamilies()` 方法,该方法会返回一个包含所有字体家族的数组。例如: ```java Typeface[] typefaces = Typeface.getFamilies(); for (Typeface typeface : typefaces) { String familyName = typeface.getFamilyName(); Log.d(TAG, "Font family name: " + familyName); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值