自定义类:
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文件中引用
自定义TextView控件
本文介绍了一个自定义的TextView控件实现方式,该控件能够根据应用的状态改变字体样式。文章详细展示了如何在不同构造函数中初始化控件,并在Application类中加载字体资源。

被折叠的 条评论
为什么被折叠?



