所以在问这个问题之前,我先做了一些研究,因为我知道有很多关于这个主题的文章。但是我发现有关更改整个应用程序字体的解决方案仅限于textview,而不是例如动作栏和菜单。我也知道如何更改这些字体(在每个活动中),但是这是在运行时完成的,因此当我使用以下代码(例如)更改菜单项的字体时,我注意到文本从默认字体更改到自定义字体。我的问题是,是否有更好的方法可以做到这一点
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mainpage, menu);
getLayoutInflater().setFactory(new Factory() {
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name.equalsIgnoreCase("TextView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
if(MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
((TextView) view).setTypeface(font);
((TextView) view).setTextSize(22);
}
}});
return view;
} catch (Exception e) {Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());}
}
if (name.equalsIgnoreCase("Button")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
if(MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
((Button) view).setTypeface(font);
((Button) view).setTextSize(22);
}
}
});
return view;
} catch (Exception e) { Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());}
}
return null;
}
});
return true;
}
由于我没有答案,因此我将发布解决自定义字体出现之前先加载默认字体的问题的解决方法。(在菜单项中...)我将代码放在OnCreate方法中,并删除了运行功能。
LayoutInflater layoutInflater = getLayoutInflater();
final LayoutInflater.Factory existingFactory = layoutInflater.getFactory();
// use introspection to allow a new Factory to be set
try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
getLayoutInflater().setFactory(new LayoutInflater.Factory() {
public View onCreateView(String name, Context context,
AttributeSet attrs) {
if (name.equalsIgnoreCase("TextView")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
// new Handler().post(new Runnable() {
// public void run() {
if (MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
((TextView) view).setTypeface(font);
((TextView) view).setTextSize(22);
}
// }
// });
return view;
} catch (Exception e) {
Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());
}
}
if (name.equalsIgnoreCase("Button")) {
try {
LayoutInflater li = LayoutInflater.from(context);
final View view = li.createView(name, null, attrs);
// new Handler().post(new Runnable() {
// public void run() {
// // set the text color
if (MainActivitySharedPref.GetValue(getApplicationContext(), "Language").equals("ar")) {
Typeface font = Typeface.createFromAsset(getAssets(), getString(R.string.arabic_font_name));
((Button) view).setTypeface(font);
((Button) view).setTextSize(22);
}
// }
// });
return view;
} catch (Exception e) {
Log.e(TAG, "[ERROR] an InflateException was thrown: " + e.getMessage());
}
}
return null;
}
});
}catch(Exception e){}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句