Android 中使用第三方自定义字体

 1)概述:

安卓上使用自定义的字体比较简单,在assets文件夹下面,自己定义一个font文件夹,然后,把自己的字体放进去,对相应的TextView设置自定义字体

Typeface typeface = Typeface.createFromAsset(instance.getAssets(), fonts/xxx.ttf);
textView.setTypeface(typeface);

2)使用:

第一步:在自定义的MyApplication中初始化字体调用方法

public class MyApplication extendsApplication {
    private Typeface typeface;
    private static MyApplication _instance;

    @Override
    public void onCreate() {
        super.onCreate();
        _instance = (MyApplication) getApplicationContext();
        typeface = Typeface.createFromAsset(_instance.getAssets(), fonts/xxx.ttf);
    }

    public static  MyApplication getInstace() {
        return_instance;
    }

    public Typeface getTypeface() {
        returntypeface;
    }

    public void setTypeface(Typeface typeface) {
        this.typeface = typeface;
    }
}

第二步:View对其引用:

public class MyTextView extendsTextView {

    public MyTextView(Context context) {
        super(context);
        setTypeface(MyApplication.getInstace().getTypeface());
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(MyApplication.getInstace().getTypeface());
    }

    public MyTextView(Context context, AttributeSet attrs, intdefStyleAttr) {
        super(context, attrs, defStyleAttr);
        setTypeface(MyApplication.getInstace().getTypeface());
    }

}

3)结语:

完毕!谢谢阅读

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值