我的 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);
除列表适配器外,所有东西都按预期工作,当我更改应用程序语言时,所有东西都获得了语言更改效果,但列表适配器没有。
我有两种语言的资源。
我正在通过这种方法更改语言:
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 的当前问题。
因为 conf.locale = myLocale;
在 API 级别 24 中被弃用。
所以我没有直接使用conf.locale = myLocale;
。使用 getLocales()
和 setLocales(LocaleList)
。如果只需要主要语言环境,getLocales().get(0)
现在是首选访问器。
此外,updateConfiguration
在 API 级别 25 中已弃用,我使用了 createConfigurationContext (Configuration overrideConfiguration)
相反。
但它没有用,我是否遗漏了什么?
最佳答案
在将 Activity context
而不是 applicationContext
传递给 spinnerAdapter
后,此问题得到解决。
只是我将 spinnerAdapter = new SpinnerAdapter(getApplicationContext());
更改为
spinnerAdapter = new SpinnerAdapter(MainActivity.this);
A REF .
关于android - 该应用程序不会在 nougat api 7++ 上获得本地化更改效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46531579/