自定义TextView支持第三方字体库(以隶书为例)

下载地址:

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" />

 

请多指教,欢迎留言。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值