【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">

完成~

相关推荐
Ehtan_Zheng18 分钟前
让你的代码更整洁:10 个必知的 Kotlin 扩展函数
android
城东米粉儿27 分钟前
Android VSync 笔记
android
城东米粉儿29 分钟前
Android SurfaceFlinger 笔记
android
似霰37 分钟前
Android 日志系统5——logd 写日志过程分析二
android·log
hewence11 小时前
Kotlin CoroutineContext 详解
android·开发语言·kotlin
Albert Edison1 小时前
【Python】文件
android·服务器·python
大模型玩家七七2 小时前
效果评估:如何判断一个祝福 AI 是否“走心”
android·java·开发语言·网络·人工智能·batch
Aurora4192 小时前
Android事件分发逻辑--针对事件分发相关函数的讲解
android
似霰2 小时前
Android 日志系统4——logd 写日志过程分析一
android
youyoulg3 小时前
利用Android Studio编译Android上可直接执行的二进制
android·ide·android studio