产品狗你来吧(一)——关于项目中使用第三方字体的自定义TextView

来源:互联网 发布:华为网络电话机 编辑:程序博客网 时间:2024/06/09 16:28

如果在App设计过程中,有需求要用第三方的ttf字体,那么在项目初期,最好使用一个自定义的TextVIew来做这件事,以免哪天产品狗突然发疯让你换字体的话 ,在项目中一个一个的TextView的去换TypeFace可是要了亲命了.......

那么,如何写这个自定义的TextView呢?看代码........

package com.hackvg.android.views.custom_views;import android.content.Context;import android.graphics.Typeface;import android.util.AttributeSet;import android.widget.TextView;/** * Created by saulmm on 25/01/15. */public class LobsterTextView extends TextView {    public LobsterTextView(Context context) {        super(context);        init(context);    }    public LobsterTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public LobsterTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context);    }    private void init(Context context) {        Typeface t = Typeface.createFromAsset(context.getAssets(), "Lobster-Regular.ttf");        this.setTypeface(t);    }}
然后,把你下载好的TTF文件放到assets文件夹下就好了,以后如果要换字体,只要把自定义的TextView中的字体换掉即可。

是不是很安心呢?
产品狗 你来吧!

0 0
原创粉丝点击