自定义类:
public class CustomTextView extends TextView { private static final String TAG = "CustomTextView"; public CustomTextView(Context context) { super(context); init(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context); } private void init(Context context) { if (UILApplication.font != null) { setTypeface(UILApplication.font); } else { //读取assets AssetManager assetManager = context.getAssets(); Typeface font = Typeface.createFromAsset(assetManager, "fonts/apple_blackbody.ttf"); setTypeface(font); } }}
在Application中添加
public class UILApplication extends Application { public static Typeface font = null; @Override public void onCreate() { super.onCreate(); AssetManager assetManager = getAssets(); font = Typeface.createFromAsset(assetManager,"fonts/apple_blackbody.ttf"); }最后在XML文件中引用