关于安卓引入外部字体的一种解决办法

比如说,一个工程中需要所有的TextView的字体是安卓系统没有的字体,那么可以引入外部字体文件,引入方法不介绍了,

但是在AndroidStudio开发环境需要强调一点的是asserts文件夹是在main目录下的。。。。。


可以自定义该类:

public class StyledTextView extends TextView {

    public static Typeface fontFace;

    public StyledTextView(Context context) {
        this(context, null);
    }

    public StyledTextView(Context context, AttributeSet attrs) {
        this(context,attrs,0);

    }
    public StyledTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (fontFace == null) {
            fontFace = Typeface.createFromAsset(getContext().getAssets(),
                    "fonts/third_version.ttf");
        }
        setTypeface(fontFace);

    }
}

实例化该自定义控件时会自动加载字体‘;

但是很容易出现如下问题,出现如下问题首先检查asserts文件夹是不是在main目录下,如果还是不行再看如下解决方案:



  Caused by: java.lang.RuntimeException: native typeface cannot be made
            at android.graphics.Typeface.<init>(Typeface.java:325)
            at android.graphics.Typeface.createFromAsset(Typeface.java:299)



这种问题一半是字体文件过大导致,可以用如下方法缩减字体,我觉得这招挺牛的:


http://blog.csdn.net/xujixian6272002/article/details/20563535?utm_source=tuicool&utm_medium=referral







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值