0

我正在尝试让我的应用程序支持阿拉伯语和英语
我做到了,但它在某些 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());
    }
}

如果有人可以帮我一把,他会
提前感谢我的一天

4

1 回答 1

1

最后我想出了一个解决方案..
如果有人想要它会分享它

@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());
    }
}


我希望这对任何人都有帮助

于 2018-05-02T13:11:40.283 回答