xml中修改字体

1、右键点击 res 文件夹,然后转到 New > Android resource directory

2、在 Resource type 列表中,选择 font,然后点击 OK

image.png
3、在 font 文件夹中添加字体文件

关于字体,推荐两个免费下载的网站
[谷歌](https://fonts.google.com/)
[1001freefonts](https://www.1001freefonts.com/)
复制代码

image.png
ps:命名必须小写字母或数字,可包含_,其他会报红。

添加之后就会生成 R.font.ma_shan_zhenng_regular 和 R.font.noto_sans_bold

4、双击字体文件可预览当前字体

image.png
5、创建 font family

1)、右键点击 font 文件夹,然后转到 New > Font resource file。此时将显示 New Resource File 窗口。

2)、输入文件名,然后点击 OK。新的字体资源 Xml 会在编辑器中打开。

3)、将各个字体文件、样式和粗细属性都封装在 元素中。如下:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:ignore="UnusedAttribute">

    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/ma_shan_zheng_regular"
        tools:ignore="UnusedAttribute" />

    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/noto_sans_bold"
         />
</font-family>
复制代码

6.直接在布局 Xml 中使用 fontFamily 属性进行引用,如下图:

image.png

参考链接:juejin.cn/post/697438…

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享