实现全局设置字体方案1:
1:设置application的style:
<style name="TvApplicationTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:typeface">normal</item> </style> 2:
android:theme="@style/TvApplicationTheme"
3:在自定义application中设置字体
/** * 通过反射方法设置app全局字体 */ public void setTypeface() { typeFace = Typeface.createFromAsset(getAssets(), "fonts/SourceHanSerifCN-Bold.ttf"); try { Field field = Typeface.class.getDeclaredField("DEFAULT"); field.setAccessible(true); field.set(null, typeFace); LogUtils.logd(TAG, "setTypeface: "); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
4:对特定的textview设置:
boldTextView.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
实现全局设置字体方案2:
public class FontTextView extends TextView{
public FontTextView(Context context) {
super(context);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private Typeface createTypeface(Context context, String fontPath) {
return Typeface.createFromAsset(context.getAssets(), fontPath);
}
@Override
public void setTypeface(Typeface tf, int style) {
super.setTypeface(createTypeface(getContext(),"fonts/fzzy.ttf"), style);
}
}