美文网首页APP端
Android 全局替换字体

Android 全局替换字体

作者: ChenME | 来源:发表于2019-03-26 10:03 被阅读0次

1. 替换全局字体

  1. 需要引入依赖
// 替换全局字体使用
api 'com.android.support:support-v13:28.0.0'
  1. res 目录下创建一个 font 目录,将用到的字体复制进去,然后再创建一个字体的xml。
替换字体01.jpg
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        android:font="@font/pop_ttf"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/pop_ttf"
        app:fontStyle="normal"
        app:fontWeight="400" />

    <font
        android:font="@font/pop_ttf"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/pop_ttf"
        app:fontStyle="italic"
        app:fontWeight="400" />
</font-family>
  1. 在主题中引用
<!-- 主题样式 -->
<style name="BaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <!-- 其他配置 -->
 
    <!-- 设置全局的字体样式 -->
    <item name="android:fontFamily">@font/pop_font</item>
</style>

2. 在实际使用中,发现对 RadioButton 不起作用,所以自定义一个 RadioButton 单独对其进行处理;

  1. 添加属性;
<!-- 自定义 RadioButton -->
<declare-styleable name="MyRadioButton">
    <attr name="textFont" format="reference" />
</declare-styleable>
  1. 继承 RadioButton;
class MyRadioButton @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : RadioButton(context, attrs, defStyleAttr) {

    private var textFont: Int = -1

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRadioButton) //获取我们定义的属性
        textFont = typedArray.getResourceId(R.styleable.MyRadioButton_textFont, -1)

        initView()
        typedArray.recycle()
    }

    private fun initView() {
        if (-1 != textFont) {
            paint.isAntiAlias = true
            val font = ResourcesCompat.getFont(context, textFont)
            paint.typeface = font
        }
    }
}
  1. 使用;
<mm.cme.baselibrary.widgets.MyRadioButton
    android:id="@+id/rb_mine"
    style="@style/MainBtmTabRBtn"
    android:text="我的"
    app:textFont="@font/pop_font" />

相关文章

  • Android 全局替换字体

    1. 替换全局字体 需要引入依赖 在 res 目录下创建一个 font 目录,将用到的字体复制进去,然后再创建一个...

  • Android App全局字体替换

    【注】 https://blog.csdn.net/u011840744/article/details/5076...

  • Android实现全局替换字体

    目录 效果展示 实现步骤 原生替换字体1.创建MyApplication并在AndroidManifest中引用 ...

  • android更换字体

    android更换字体 最近在研究android端字体替换需求,发现还是有很多方式来替换字体。 一,前言 Andr...

  • Android之全局替换字体库

    Android O推出了一项新的功能「Fonts in XML」(中文翻译),借助这项功能,我们能够像使用其他资源...

  • iOS字体全局替换

    产品让把APP中的字体替换为Roboto,首先想到给UIFont添加分类,利用runtime进行方法替换,将使用s...

  • 粗暴的方式,替换全局字体

    序 在 Android 下使用自定义字体已经是一个比较常见的需求了,最近也做了个比较深入的研究。 那么按照惯例我又...

  • 通过修改 LayoutInflater,全局替换字体!!!

    序 在 Android 下使用自定义字体已经是一个比较常见的需求了,最近也做了个比较深入的研究。 那么按照惯例我又...

  • Android 全局设置字体

    Android 8.0 和 Android 支持库 26 允许您从提供程序应用请求字体,而无需将字体绑定到 APK...

  • Android 全局设置字体

    说到这,我估计你心理已经有全新的解决方案了,那就是利用setFactory,相关代码如下(在BaseActivit...

网友评论

    本文标题:Android 全局替换字体

    本文链接:https://www.haomeiwen.com/subject/pvpwvqtx.html