The app doesn't get the localization change effect on nougat api 7 ++

社会主义新天地 提交于 2019-11-28 09:51:52

问题


I have a custom adapter for my spinner:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item) {

      .............

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter.add(context.getResources().getString(R.string.value1));
    adapter.add(context.getResources().getString(R.string.value2));
    adapter.add(context.getResources().getString(R.string.hint));
    spinner.setAdapter(adapter);

Every thing work as expect but list adapter, when i change the application language every thing got the language change effect but the list adapter don't.

I have the resource for both language.

I'm changing language via this method:

public  void setLocale(String lang) {
    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);

}

After i test the case on lower device like lolipop it's worked as well, the current issue with android oreo 8.0.

Since conf.locale = myLocale; was deprecated in API level 24.

So i didn't use conf.locale = myLocale;directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

Also updateConfiguration was deprecated in API level 25, I used createConfigurationContext (Configuration overrideConfiguration) instead.

But it didn't work, Is i missing something?


回答1:


This issue fixed after passing the Activity context instead of applicationContext to the spinnerAdapter.

Simply I changed spinnerAdapter = new SpinnerAdapter(getApplicationContext()); to spinnerAdapter = new SpinnerAdapter(MainActivity.this);

A REF.



来源:https://stackoverflow.com/questions/46531579/the-app-doesnt-get-the-localization-change-effect-on-nougat-api-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!