在 Android 中更改整个应用程序的字体
Changing Font For Entire App In Android
所以我在问这个问题之前先做了一些研究,因为我知道有很多关于这个主题的文章。
但是我发现关于更改整个应用程序字体的解决方案仅限于文本视图,而不是操作栏和菜单等。我也知道如何更改这些字体(在每个 activity 中),但这是在 运行 时间完成的,所以当我使用下面的代码更改菜单项的字体时,我注意到文本从默认字体更改为自定义字体。
我的问题是,有没有更好的方法来做到这一点
@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;
}
由于我没有得到答复,我将 post 我所做的工作来解决在自定义字体出现之前加载默认字体几毫秒的问题。 (在菜单项中...)
我将代码放在 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){}
所以我在问这个问题之前先做了一些研究,因为我知道有很多关于这个主题的文章。 但是我发现关于更改整个应用程序字体的解决方案仅限于文本视图,而不是操作栏和菜单等。我也知道如何更改这些字体(在每个 activity 中),但这是在 运行 时间完成的,所以当我使用下面的代码更改菜单项的字体时,我注意到文本从默认字体更改为自定义字体。 我的问题是,有没有更好的方法来做到这一点
@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;
}
由于我没有得到答复,我将 post 我所做的工作来解决在自定义字体出现之前加载默认字体几毫秒的问题。 (在菜单项中...) 我将代码放在 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){}