我试图让我的应用支持阿拉伯语&英语 我做到了,但它在某些Android版本idk中效果不佳......
我使用以下代码来创建应用程序的平均活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String localLanguage = MySingleton.getmInstance(this).getAppLang();
if (localLanguage.equals(getString(R.string.settings_language_arabic_value))) {
Locale localeAr = new Locale("ar");
setLocale(localeAr, getString(R.string.settings_language_arabic_value));
} else if (localLanguage.equals(getString(R.string.settings_language_english_value))) {
Locale localeEn = new Locale("en");
setLocale(localeEn, getString(R.string.settings_language_english_value));
}
setContentView(R.layout.activity_branches);
.......
.......
}
private void setLocale(Locale locale, String lang) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
if (lang.equals(getString(R.string.settings_language_arabic_value)))
config.setLayoutDirection(new Locale("ar"));
else config.setLayoutDirection(new Locale("en"));
getApplicationContext().getResources().updateConfiguration(config, null);
} else {
Configuration config = new Configuration();
config.locale = locale;
if (lang.equals(getString(R.string.settings_language_arabic_value)))
config.setLayoutDirection(new Locale("ar"));
else config.setLayoutDirection(new Locale("en"));
Locale.setDefault(config.locale);
Resources resources = getResources();
resources.updateConfiguration(config, resources.getDisplayMetrics());
}
}
如果有人可以帮助我,他将会成为我的一天 提前谢谢
答案 0 :(得分:0)
最后我想出了一个解决方案..
如果有人想要的话,我们会分享它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String localLanguage = MySingleton.getmInstance(this).getAppLang();
if (localLanguage.equals(getString(R.string.settings_language_arabic_value))) {
setLocale("ar", this);
} else if (localLanguage.equals(getString(R.string.settings_language_english_value))) {
Locale localeEn = new Locale("en");
setLocale("en", this);
}
setContentView(R.layout.activity_branches);
.........
.........
}
代码的变化如下:
private void setLocale(String lang, Context context) {
Configuration config = context.getResources().getConfiguration();
Locale locale = new Locale(lang);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
getApplicationContext().getResources().updateConfiguration(config, null);
} else {
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
config.setLayoutDirection(locale);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
context.createConfigurationContext(config);
} else {
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
}
我希望这有助于任何人