Android自定义字体

作者: i小灰 | 来源:发表于2021-03-02 10:30 被阅读0次

文章目的:快速入门Android中自定义各种字体!

前言:我们都知道,Android中默认的字体是黑体,而大多数app也都是使用的这种字体,但我们发现,大多数app中,个别地方字体非常好看,例如app的标题栏,菜单栏等地方,那他们是怎么做到的呢?有两种方式,第一是图片来代替文字,第二,就是今天我要教大家的自定义字体。

一 自定义字体

说到字体,我们不难联想到我们使用office时可以选择的各种字体,我们就是需要这种字体文件,值得一提的是,Windows提供了很多字体文件,可以在C:\Windows\Fonts找到。当然,我们也可以去网络上下载你喜欢的字体文件。字体文件是ttf格式的哟。

那我们现在就开始,我们先把要使用的字体文件放入到工具中,操作如下:

(1)新建一个名叫assets的文件夹,然后把字体文件复制到里面


image.png image.png

Mogra-Regular.ttf就是字体文件

(2)我们新建一个类,名叫FontCustom,写入代码:

import android.content.Context;
import android.graphics.Typeface;

/**
 * author : i小灰
 * desc   :自定义字体
 */
public class FontCustom {

    // fongUrl是自定义字体分类的名称
    private static String fongUrl = "Mogra-Regular.ttf";
    //Typeface是字体,这里我们创建一个对象
    private static Typeface tf;

    /**
     * 设置字体
     */
    public static Typeface setFont(Context context)
    {
        if (tf == null)
        {
            //给它设置你传入的自定义字体文件,再返回回来
            tf = Typeface.createFromAsset(context.getAssets(),fongUrl);
        }
        return tf;
    }
}

(3)新建一个类名叫MyTextView继承TextView,重写2个参数的构造方法

import android.content.Context;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * author : i小灰
 * desc   :自定义字体
 */
public class MyTextView extends AppCompatTextView {
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
    /**
     * 初始化字体
     * @param context
     */
    private void init(Context context) {
        //设置字体样式
        setTypeface(FontCustom.setFont(context));
    }
}

使用自定义字体类

我们复制MyTextView的路径到activity_main中,替换原有的TextView,我这里的路径是
com.my.customfont.font.MyTextView

修改activity_main中的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.my.customfont.font.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello 我是i小灰!"
        android:textSize="25sp" />

</LinearLayout>

好了,到这里,我们就完成了自定义字体,现在我们运行程序看看效果!

customfont.jpg

效果很酷炫,对不对?我们只需要2个类就可以完成了自定义字体,之后再哪里需要使用自定义字体,就把路径替换原有的TextView就完成了!

字体地址

总结:
自定义字体在我们的程序中其实用的地方不多,大多数时候,我们都喜欢用图片来代替TextView来作为标题名称等特殊地方。如果我们在程序中展示的文字内容,使用自定义字体,那就是非常棒的选择,会给人一种耳目一新的感觉。

项目下载地址:https://pan.baidu.com/s/1Av_64Xz3z2Uf6BWPiNS3yw 提取码:1234
分享一个flutter项目,作为学习 OpenJMU

欢迎关注 i小灰 带你学习新知识!

相关文章

  • android自定义字体

    Android O通过字体资源支持自定义字体,支持.otf(OpenType)和.ttf(TrueType)字体格...

  • Android自定义字体

    文章目的:快速入门Android中自定义各种字体! 前言:我们都知道,Android中默认的字体是黑体,而大多数a...

  • Android自定义字体设置

    Android自定义字体设置 1.typeface、fontFamily、textStyle介绍 1.typefa...

  • Android 自定义字体

    说明 在一些应用中,有改变字体,或者某些页面有特殊字体要求时,Android自身的字体满足不了时,需要自定义字体,...

  • Android自定义字体

    Android 自定义字体 1. Android默认方法 为每个view单独设置Typeface. 这种方法可以扩...

  • Android上的自定义字体 - 通过XML进行动态字体选择

    前言 这是我们系列自定义字体在Android上的第2篇文章。在之前,我们已经看到如何使用自定义字体并将不同的样式应...

  • Android 自定义字体方案

    在应用中需要配置不同的字体,而 Android 只能在 xml 中配置系统默认提供的四种字体,需要自定义的字体都需...

  • Android APP支持自定义字体

    情景:需要为整个应用替换自定义字体。 Android对于文字的字体设置主要是通过以下两个对象 FontFamily...

  • Android 设置字体

    吐槽 首先,来点吐槽。在Android O 之前设置字体是如此之…… 缺点:自定义字体只能通过动态代码设置; 每次...

  • 十九、Expo 使用自定义字体

    使用自定义字体 iOS和Android都有自己的平台字体集,但如果你想在你的应用中注入更多的品牌个性,那么选择好的...

网友评论

    本文标题:Android自定义字体

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