Android—解决重写TextView设置字体大量引用后卡屯的问题

来源:互联网 发布:mac推出键 编辑:程序博客网 时间:2024/05/17 22:44

我在http://blog.csdn.net/weiguishan/article/details/46678935这边博客谢了两种方法,自定义textview字体,在xml里面大量引用会卡顿,下面给出解决办法。

首先写一个类,继承Application,在该类的oncreate方法获取Typeface

public class BusinessApplication extends Application {

public static Typeface texttypeface = null;
public static Typeface digitaltypeface = null;
@Override
public void onCreate() {
//生成字体库文件
texttypeface = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/weiruanyahei.ttf");
}

}

然后,在继承TextView的自定义类里面

package com.insightcode.androidfont;
import com.insightcode.app.BusinessApplication;


import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
 * 文本为汉子,设置微软雅黑
 *
 */
public class FontTextTextView extends TextView {
    private Context mContext=null;
    Typeface texttypeface=null;

    public FontTextTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(mContext==null){
            this.mContext = context;
        }
        if(texttypeface==null){
            texttypeface = BusinessApplication.texttypeface;
        }
        this.setTypeface(texttypeface);
    }




    public FontTextTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(mContext==null){
            this.mContext = context;
        }
        if(texttypeface==null){
            texttypeface = BusinessApplication.texttypeface;
        }
        this.setTypeface(texttypeface);
    }




    public FontTextTextView(Context context) {
        super(context);
        if(mContext==null){
            this.mContext = context;
        }
        if(texttypeface==null){
            texttypeface = BusinessApplication.texttypeface;
        }
        this.setTypeface(texttypeface);
      }
}


在xml里面引用即可


0 0
原创粉丝点击