比如说,一个工程中需要所有的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