该应用程序在 nougat api 7 ++ 上没有获得本地化更改效果 [英] The app doesn't get the localization change effect on nougat api 7 ++
问题描述
我的 spinner
有一个自定义适配器:
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'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);
}
我在像 lolipop 这样的低端设备上测试了这个案例后,它也能正常工作,这是 android oreo 8.0 的当前问题.
After i test the case on lower device like lolipop it's worked as well, the current issue with android oreo 8.0.
因为 conf.locale = myLocale;
在 API 级别 24 中已弃用.
Since conf.locale = myLocale;
was deprecated in API level 24.
所以我没有直接使用 conf.locale = myLocale;
.使用 getLocales()
和 setLocales(LocaleList)
.如果只需要主要语言环境,getLocales().get(0)
现在是首选访问器.
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.
另外 updateConfiguration
在 API 级别 25 中被弃用,我使用了 createConfigurationContext (Configuration overrideConfiguration)
代替.
Also updateConfiguration
was deprecated in API level 25, I used createConfigurationContext (Configuration overrideConfiguration)
instead.
但是没有用,我是不是遗漏了什么?
But it didn't work, Is i missing something?
推荐答案
在将 Activity context
而不是 applicationContext
传递给 spinnerAdapter代码>.
This issue fixed after passing the Activity context
instead of applicationContext
to the spinnerAdapter
.
我只是将 spinnerAdapter = new SpinnerAdapter(getApplicationContext());
更改为spinnerAdapter = new SpinnerAdapter(MainActivity.this);
参考资料.
这篇关于该应用程序在 nougat api 7 ++ 上没有获得本地化更改效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!