将语言更改为Android 6.0.1时遇到问题,在新的Android版本上,更改语言效果很好,但是在6.0.1上设置默认字符串,无论设备设置的语言如何。该模拟器可以运行,但是当我在Samsung J5上安装apk时,它在Android 6.0.1上可以正常工作,更改语言无法正常工作。我的问题有什么解决办法吗?谢谢。
private void setLocale(String lang) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config =
getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
Resources resources = getBaseContext().getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(new Locale(lang));
getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}
// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
}
答案 0 :(得分:2)
如果您使用 aap 分发您的应用,请确保确保所有语言字符串都捆绑在用户从 Google Play 下载的 apk 中。
您可以通过添加以下内容来更新您的应用 build.gradle:
android {
bundle {
language {
// Specifies that the app bundle should not support
// configuration APKs for language resources. These
// resources are instead packaged with each base and
// dynamic feature APK.
enableSplit = false
}
}
}
答案 1 :(得分:1)
这对我不起作用。我尝试了所有解决方案,但无法在Android 6.0.1上运行
我正在使用appCompat androidx.appcompat:appcompat:1.1.0
请阅读此https://stackoverflow.com/a/58004553/3452078
我使用了下面的代码,并且在所有版本上都可以正常运行
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}
答案 2 :(得分:0)
请检查,可能对下面的链接有用:
https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758
答案 3 :(得分:0)
config.locale已过时。您需要使用config.setLocale并在N以下添加DisplayMetrics。
在this answer.中尝试解决方案。
编辑: 试试这个
private void setLocale(String lang) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config =
getBaseContext().getResources().getConfiguration();
//config.locale = locale;
config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
Resources resources = getBaseContext().getResources();
Configuration configuration = resources.getConfiguration();
//configuration.setLocale(new Locale(lang));
configuration.locale = new Locale(lang);
getBaseContext().getApplicationContext().createConfigurationContext(configuration);
}
// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
}
答案 4 :(得分:0)
由于我设法编辑了代码,因此可能要结束本主题,也许有人需要它。
编辑
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= 24) {
config.setLocale(locale);
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} else {
config.locale = locale;
getBaseContext().getApplicationContext().createConfigurationContext(config);
}
// shared pref.
SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
editor.putString("My_Lang", lang);
editor.apply();
}
答案 5 :(得分:0)
2021 年 3 月
我使用了下面的代码,它在所有版本上都能完美运行。
完整代码:
注意:对于 6.0.1 版本必须在 setContentView(R.layout.activity_main)
之前更新区域设置,对于 6.0.1 的更高版本(API 级别 >=24)需要覆盖 protected void attachBaseContext(Context newBase)
您项目的所有活动。
主活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
ContextUtils.updateLocale(MainActivity.this, ContextUtils.getSavedLanguage(MainActivity.this));
}
setContentView(R.layout.activity_main);
......
.....
}
@Override
protected void attachBaseContext(Context newBase) {
String localeToSwitchTo= ContextUtils.getSavedLanguage(newBase);
ContextWrapper localeUpdatedContext = ContextUtils.updateLocale(newBase, localeToSwitchTo);
super.attachBaseContext(localeUpdatedContext);
}
ContextUtils
public class ContextUtils extends ContextWrapper {
public ContextUtils(Context base) {
super(base);
}
public static ContextWrapper updateLocale(Context context, String lang) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale localeToSwitchTo = new Locale(lang);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(localeToSwitchTo);
LocaleList localeList = new LocaleList(localeToSwitchTo);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
}else {
Locale.setDefault(localeToSwitchTo);
configuration.locale=localeToSwitchTo;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
return new ContextUtils(context);
}
public static String getSavedLanguage(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
context.getApplicationContext());
return preferences.getString("SETTINGS_LANG", "en");
}
public static void setSavedLanguage(Context context, String lang){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
context.getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("SETTINGS_LANG", lang);
editor.apply();
}
}