【Android】字体设置

需求 :

  1. 全局字体设置 2. 局部字体设置
    既可通过kotlin或者第三方库实现, 也可以android ui上直接实现
    此处讲的是使用Android自带的字体设置

实现 :

1. 字体文件放到src/main/res/font 目录下

2. 局部字体使用 ↓

xml 复制代码
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2.3"
                android:text="Name"
                android:textColor="@color/progress_secondary_color"
                android:textSize="15sp"
                android:fontFamily="@font/gilroy_medium_2"
                />

3. 全局字体使用

3.1 main/res/values/styles.xml 定义全局字体

xml 复制代码
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:fontFamily">@font/your_custom_font</item>
</style>

3.2 AndroidManifest.xml 引用style

xml 复制代码
    <application
        android:name=".TestApplication"
        android:allowBackup="true"
        android:appComponentFactory="@string/app_name"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning"
        tools:replace="android:appComponentFactory">

完成~

相关推荐
杉氧8 小时前
100% Kotlin:基于 KMP + Compose Multiplatform 的全栈架构实战(Clean Architecture + MVI)
android·架构
小仙女喂得猪9 小时前
AI 写 Android 代码老翻车?我把移动端的 Harness 系统开源了
android·github·ai编程
杉氧9 小时前
第一篇:从一个 Dagger 报错开始:手把手带你搭建 Hilt 依赖注入的护城河
android·架构
咋吃都不胖lyh9 小时前
短期记忆和长期记忆都存 MySQL
android·java·开发语言
杊页11 小时前
系列三:组件化与模块化进阶 | 第8篇 组件化与模块化核心实战区别:大型项目架构的必由之路
android·android jetpack
曲幽12 小时前
旧手机别扔!用 Termux 搭个私人云盘,比网盘香多了
android·termux·alist·filebrowser
Kapaseker13 小时前
Android 开发来看看 Kotlin 2.4.0 更新了个啥
android·kotlin
前端与小赵13 小时前
快速生成安卓证书并打包生成安卓apk(保姆教程)
android·前端
坚果的博客13 小时前
鸿蒙PC三方库适配OAT.xml 与 SHA512SUM 解读:开源合规与源码校验
xml·开源·harmonyos