问题
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