Java 按钮和操作栏忽略应用程序中定义的自定义字体
使用以下代码覆盖应用程序中的字体,但ActionBar和按钮会忽略它。其余的(文本视图和编辑文本工作正常)。我正在使用AppCompat库。提前谢谢你的帮助Java 按钮和操作栏忽略应用程序中定义的自定义字体,java,android,fonts,Java,Android,Fonts,使用以下代码覆盖应用程序中的字体,但ActionBar和按钮会忽略它。其余的(文本视图和编辑文本工作正常)。我正在使用AppCompat库。提前谢谢你的帮助 @Override public void onCreate() { super.onCreate(); final Typeface regular = Typeface.createFromAsset(getAssets(),"fonts/avenir.ttc"); replaceFont(Typeface
@Override
public void onCreate() {
super.onCreate();
final Typeface regular = Typeface.createFromAsset(getAssets(),"fonts/avenir.ttc");
replaceFont(Typeface.defaultFromStyle(R.style.AppTheme).toString(), regular);
replaceFont("DEFAULT", regular);
}
protected static void replaceFont(String staticTypefaceFieldName,
final Typeface newTypeface) {
if (Build.VERSION.SDK_INT >= 21) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("sans-serif", newTypeface);
try {
final Field staticField = Typeface.class
.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
Field sDefaults = Typeface.class.getDeclaredField("sDefaults");
sDefaults.setAccessible(true);
sDefaults.set(null, new Typeface[]{
newTypeface, newTypeface, newTypeface, newTypeface
});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
try {
final Field staticField = Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
@覆盖
public void onCreate(){
super.onCreate();
final Typeface regular=Typeface.createFromAsset(getAssets(),“fonts/avenir.ttc”);
replaceFont(Typeface.defaultFromStyle(R.style.AppTheme.toString(),常规);
replaceFont(“默认”,常规);
}
受保护的静态void replaceFont(字符串staticTypefaceFieldName,
最终字体(新字体){
如果(Build.VERSION.SDK_INT>=21){
Map newMap=newhashmap();
newMap.put(“无衬线”,newTypeface);
试一试{
最终字段staticField=Typeface.class
.getDeclaredField(“sSystemFontMap”);
staticField.setAccessible(true);
staticField.set(null,newMap);
字段sDefaults=Typeface.class.getDeclaredField(“sDefaults”);
sDefaults.setAccessible(true);
sDefaults.set(空,新字体[]){
newTypeface,newTypeface,newTypeface,newTypeface
});
}捕获(无此字段例外){
e、 printStackTrace();
}捕获(非法访问例外e){
e、 printStackTrace();
}
}否则{
试一试{
最终字段staticField=Typeface.class
.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
staticField.set(null,newTypeface);
}捕获(无此字段例外){
e、 printStackTrace();
}捕获(非法访问例外e){
e、 printStackTrace();
}
}
}
尝试使用这个类,我在堆栈溢出本身中发现了它
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
谢谢你用这个
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
我不确定它是否适用于按钮尝试:)尝试使用这个类我在堆栈溢出本身中发现了这个
public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);
private Typeface mTypeface;
/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);
if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));
// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}
谢谢你用这个
SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
我不确定它是否适用于按钮尝试:)如果有人解释它是如何工作的,那就太好了。(我想问题在于按钮和ActionBar没有继承某个类,而该类会覆盖字体)。谢谢。如果有人解释一下它是如何工作的,那就太好了。(我想问题在于按钮和ActionBar没有继承某个类,而该类会覆盖字体)。谢谢。谢谢你的回复,但我真的在寻找一种解决方案,它能让我立刻覆盖一切。我希望,谷歌能有所成就。(这是一件很普通的事情,他们真的把事情弄得太复杂了)谢谢你的回答,但我真的在寻找一种解决方案,让我可以立刻覆盖一切。我希望,谷歌能有所成就。(这么普通的事情,他们真的把事情弄得太复杂了)