Android基础之使用自定义的字体库

如何让我们的应用不受系统字体的影响呢?下面小编将介绍下如何在Android应用里面使用自定义的字体库。

前言:谨慎在listview/gridview中使用自定义字体,因为渲染耗时长,容易造成卡顿。

1、assets/fonts下面是我们要使用的字体库文件。完整路径:assets/fonts/msyh.ttf

这里写图片描述

2、核心代码是CustomMsyhTV.java。CustomMsyhTV继承自TextView,在初始化的时候读取自定义的字体库。


public class CustomMsyhTV extends TextView {

    public CustomMsyhTV(Context context) {
        super(context);
        init(context);
    }

    public CustomMsyhTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomMsyhTV(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /***
     * 设置字体
     * @return
     */
    public void init(Context context) {
        AssetManager assetMgr = context.getAssets();
        Typeface font = Typeface.createFromAsset(assetMgr,"fonts/msyh.ttf");
        setTypeface(font);
    }
}

3、然后在布局文件中,像普通的TextView一样,引用我们自定义的这个文件。

	<com.guan.o2o.view.CustomHanzTV
          android:id="@+id/tv_recom_code"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:text="@string/recom_code"
          android:textColor="@color/white"
          android:textSize="@dimen/TEXT_SIZE_16SP" 
          />

4、在Activity只是负责展示我们自定义的这个TextView。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值