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

完成~

相关推荐
LT101579744439 分钟前
2026年手机机型兼容性测试选型指南:安卓机型碎片化、APP闪退UI错乱如何解决
android·测试工具·ui·智能手机
xiangji1 小时前
开源完美模块组件化可扩展的Xml解析器Hand.ParseXml
xml·模块化·组件化·可扩展
阿pin1 小时前
Android随笔-view是如何显示到屏幕上
android·view
恋猫de小郭2 小时前
Flutter material/cupertino 解耦最新进展,已经在做新样式了
android·前端·flutter
在书中成长2 小时前
HarmonyOS 小游戏《对战五子棋》开发第17篇 - AI的两种评估策略
android·java·javascript
私人珍藏库2 小时前
[Android] Npatch-免Root框架+可内置模块
android·人工智能·智能手机·工具·软件
黄林晴3 小时前
KMP 正式支持 SwiftPM:CocoaPods 迁移配置指南
android·ios·kotlin
AFinalStone3 小时前
Android 7系统网络(二)内核层—netfilter_iptables与路由策略
android·网络
吠品3 小时前
Mac定时关机、开机自动化:用shutdown和pmset就够了
android·java·开发语言
2601_9637713719 小时前
Offloading WP-Cron and Securing Ticket Webhooks on Enterprise IT Sites
android