我有带有英语和阿拉伯语两种语言的 Android 应用程序,我像这样更改应用程序语言:
public void setLocale() {
if (SharedPreferencesHelper.getSharedPreferencesInt(this, AppConstant.LANGUAGE, AppConstant.LANGUAGE_EN_MFQ) == AppConstant.LANGUAGE_AR_MFQ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
当设备语言为英语且应用程序语言为英语时,应用程序可以完美运行,但是,当应用程序为阿拉伯语且设备为英语时,某些 fragment 会给出错误的语言,或者某些 View (例如阿拉伯语文本和英语文本)在同一 fragment ,这是我的问题,我该如何解决这个问题?
最佳答案
我无法解决您的问题,但我会告诉您我如何设法创建一个使用英语/阿拉伯语的应用程序。
我使用了这篇博文中的LocaleHelper.java
类:Change Language Programmatically in Android
并创建了一个自定义 Activity 类(覆盖基本上下文)作为应用程序中所有 Activity 的基类
public class MyAppCompatActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, LocaleHelper.LOCALE_DEFAULT_LANG));
}
}
这对我很有用,希望有帮助
关于java - 设备语言对应用程序语言的影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46755782/