产品狗你来吧(一)——关于项目中使用第三方字体的自定义TextView

本文介绍如何在Android项目中使用自定义TextView解决引入第三方ttf字体的问题,通过将字体直接放入assets文件夹,并在自定义TextView中加载,避免了逐个TextView替换字体的繁琐操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果在App设计过程中,有需求要用第三方的ttf字体,那么在项目初期,最好使用一个自定义的TextVIew来做这件事,以免哪天产品狗突然发疯让你换字体的话 ,在项目中一个一个的TextView的去换TypeFace可是要了亲命了.......

那么,如何写这个自定义的TextView呢?看代码........

package com.hackvg.android.views.custom_views;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by saulmm on 25/01/15.
 */
public class LobsterTextView extends TextView {
    public LobsterTextView(Context context) {

        super(context);

        init(context);
    }

    public LobsterTextView(Context context, AttributeSet attrs) {

        super(context, attrs);

        init(context);
    }

    public LobsterTextView(Context context, AttributeSet attrs, int defStyleAttr) {

        super(context, attrs, defStyleAttr);


        init(context);

    }

    private void init(Context context) {

        Typeface t = Typeface.createFromAsset(context.getAssets(), "Lobster-Regular.ttf");
        this.setTypeface(t);
    }
}
然后,把你下载好的TTF文件放到assets文件夹下就好了,以后如果要换字体,只要把自定义的TextView中的字体换掉即可。

是不是很安心呢?
产品狗 你来吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值