棒棒糖设置默认本地不起作用

Sac*_*per 8 android android-5.0-lollipop

我有这个方法运行在大多数Android API版本上使用语言(字符串等)设置应用程序

protected void setDefaultLocale(Context context, Locale locale) {
    Locale.setDefault(locale);
    Configuration appConfig = new Configuration();
    appConfig.locale = locale;
    context.getResources()
            .updateConfiguration(appConfig, context.getResources().getDisplayMetrics());
    System.out.println("trad" + locale.getLanguage());
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    String language = sharedPref.getString("pref_language", "he_IL");
    if (!language.equals("")) 
        setDefaultLocale(this, new Locale(language));

    super.onCreate(savedInstanceState);


}
Run Code Online (Sandbox Code Playgroud)

在棒棒糖运行设备上使用时没有任何改变.

有谁知道如何解决这个问题?

Mar*_* HC 24

回答以上工作,但仅针对语言,当您想要使用时,例如:

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.getDefault());
Run Code Online (Sandbox Code Playgroud)

以前修改默认语言环境,不再像以前的Android版本那样工作了.

改变了语言环境的初始化方式(我不知道为什么,我看了一下API,什么也没说).

因此,请更改初始化语言环境的方式:

Locale locale = Locale("en_US")
Run Code Online (Sandbox Code Playgroud)

对此:

Locale locale = new Locale("en", "US");
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力:)

希望这可以帮助将来的某个人.

干杯


Sac*_*per 0

终于找到了答案。

答案是使用“iw”而不是 he_il。

字符串语言 = sharedPref.getString("pref_language", "iw");