下载地址:
http://download.csdn.net/download/jackwolf_gao/4973584
描述:自定义TextView支持第三方字体库(以隶书为例)
思路:
1,将字体库文件放到assets/fonts 目录下
2,引用方式
Typeface mtf =Typeface.createFromAsset(context.getAssets(),"fonts/lishu.ttf");
自定义TextView代码:
publicclassTextview_Typefaces extends TextView {
// 系统自带字体
// privatestatic final int SANS = 1;
// privatestatic final int SERIF = 2;
// privatestatic final int MONOSPACE = 3;
privateinttf;
// 添加字体类型
privatestaticfinalintLISHU = 4;
publicTextview_Typefaces(Context context) {
super(context);
}
publicTextview_Typefaces(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
privatevoid init(Context context,AttributeSet attrs) {
if (attrs != null) {
TypedArraya = context.obtainStyledAttributes(attrs,R.styleable.TextView_Typefaces);
tf = a.getInt(R.styleable.TextView_Typefaces_tf,tf);
switch (tf) {
caseLISHU:
Typefacemtf = Typeface.createFromAsset(context.getAssets(),"fonts/lishu.ttf");
super.setTypeface(mtf);
break;
default:
break;
}
}
}
}
attr.xml:
<?xml version="1.0"encoding="utf-8"?>
<resources>
<declare-styleable name="TextView_Typefaces">
<attr name="tf">
<flag name="LISHU"value="4"></flag>
</attr>
</declare-styleable>
</resources>
布局文件:
<com.yarin.android.Typefaces.Textview_Typefaces
xmlns:myapp="http://schemas.android.com/apk/res/com.yarin.android.Typefaces"
Android:id="@+id/tv_lishu"
Android:text="世界你好"
Android:textSize="30sp"
myapp:tf="LISHU" />
请多指教,欢迎留言。