在 android 5.0 中更改应用程序语言不起作用
Change app language in android 5.0 doesn't work
我正在使用下面的代码在单击按钮时更改我的应用程序语言(例如,从法语更改为英语),它在 android 4.0 + 上工作正常,但在 5.0 上却不能。
Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);
Configuration configEn = new Configuration();
configEn.locale = localeEn;
getApplicationContext().getResources().updateConfiguration(configEn, null);
this.recreate();
有什么线索吗?
编辑:
这是我的清单(android:configChanges)
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|locale"
android:label="@string/app_name"
android:screenOrientation="portrait"/>
你在AndroidManifest.xml
中添加了android:configChanges="locale"
了吗?我认为问题出在您的 AndroidManifest.xml
文件中。
上查看更改区域设置的示例
尝试改变:
Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);
至此
String language = "en";
String country = "US";
Locale locale = new Locale(language , country);
我从 Udhay 获得的解决方案在用户使用所选语言更改操作栏和应用 "refreshes" 中的语言时有效。我正在使用 android 6.0.
无需将语言环境添加到 androidManifest。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Locale locale = null;
switch (item.getItemId()) {
case R.id.action_en:
locale = new Locale("en_US");
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
break;
case R.id.action_is:
locale = new Locale("is", "IS");
Toast.makeText(this, "Íslanska", Toast.LENGTH_SHORT).show();
break;
}
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
return true;
}
我的解决方案是在 activity 的
之前更改语言环境
setContentView(R.layout.layout_main);
我正在使用下面的代码在单击按钮时更改我的应用程序语言(例如,从法语更改为英语),它在 android 4.0 + 上工作正常,但在 5.0 上却不能。
Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);
Configuration configEn = new Configuration();
configEn.locale = localeEn;
getApplicationContext().getResources().updateConfiguration(configEn, null);
this.recreate();
有什么线索吗?
编辑: 这是我的清单(android:configChanges)
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|locale"
android:label="@string/app_name"
android:screenOrientation="portrait"/>
你在AndroidManifest.xml
中添加了android:configChanges="locale"
了吗?我认为问题出在您的 AndroidManifest.xml
文件中。
尝试改变:
Locale localeEn = new Locale("en_US");
Locale.setDefault(localeEn);
至此
String language = "en";
String country = "US";
Locale locale = new Locale(language , country);
我从 Udhay 获得的解决方案在用户使用所选语言更改操作栏和应用 "refreshes" 中的语言时有效。我正在使用 android 6.0.
无需将语言环境添加到 androidManifest。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Locale locale = null;
switch (item.getItemId()) {
case R.id.action_en:
locale = new Locale("en_US");
Toast.makeText(this, "English", Toast.LENGTH_SHORT).show();
break;
case R.id.action_is:
locale = new Locale("is", "IS");
Toast.makeText(this, "Íslanska", Toast.LENGTH_SHORT).show();
break;
}
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
return true;
}
我的解决方案是在 activity 的
之前更改语言环境setContentView(R.layout.layout_main);