奥利奥无法更改语言
Unable to change language in Oreo
我正在尝试在我的应用中使用阿拉伯语和英语。它在 运行 android Nougat 或更低版本的设备上运行良好。但它不适用于奥利奥设备。 API 26 中是否有一些新的代码要求?我正在使用下面的代码。
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
我将 "en" 和 "ar" 作为语言参数传递。
当您设置新的 Locale
时,您应该重新启动 Activity
。您可以使用下一段代码执行它:
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
然后您的 changeLanguage()
方法将以下一种方式查看:
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
restartActivity();
}
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
context.getApplicationContext().getResources().updateConfiguration(config,
context.getApplicationContext().getResources().getDisplayMetrics());
这对我有用。
我在研究之前和之后都遇到了同样的问题,我找到了一个解决方案。
创建自定义语言感知上下文包装器并附加到 activity 的 attachBaseContext() 方法。
@SuppressWarnings("NewApi")
public static LanguageAwareContext newLanguageAwareContext(String targetLanguage, Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale newLocale = new Locale(targetLanguage);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList.setDefault(new LocaleList(newLocale));
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
return new LanguageAwareContext(context);
}
覆盖 attachBaseContext 方法并使用您自定义的上下文包装器进行更改
@Override
protected void attachBaseContext(Context newBase) {
newBase = LanguageAwareContext.newLanguageAwareContext("en",newBase);
super.attachBaseContext(newBase);
}
是的,Android Oreo 存在语言更改问题(阿拉伯语、乌尔都语和希伯来语的字符串和布局方向), 我找到了解决方案这将帮助你解决这个问题
在LanguageContextWrapper.javaclass
public static ContextWrapper wrap(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
configuration.setLayoutDirection(locale);
context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return new LanguageContextWrapper(context);
}
在activityclass重写下面的方法
override fun attachBaseContext(base: Context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
super.attachBaseContext(LanguageContextWrapper.wrap(base, Locale.getDefault().language))
} else {
super.attachBaseContext(base)
}
}
更改语言后我们必须重新创建activity,我们需要编写以下代码
fun recreateActivity(){
if (Build.VERSION.SDK_INT in 26..27) {
if (Locale.getDefault().language == "ar" ||
Locale.getDefault().language == "iw" ||
Locale.getDefault().language == "ur")
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
else
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_LTR
}
recreate()
}
我最近在 Oreo
上遇到了与布局方向相关的问题。正在更新资源,但布局方向未更改。
下面的代码对我有用。
public static void setRTLSupportIfRequired(AppCompatActivity activity) {
if(getLanguageFromPrefs(activity).equals("ar")) {
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}else{
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
}
注意:之前我使用的是 View.LAYOUT_DIRECTION_LOCALE
,但它不适用于 Oreo
我正在尝试在我的应用中使用阿拉伯语和英语。它在 运行 android Nougat 或更低版本的设备上运行良好。但它不适用于奥利奥设备。 API 26 中是否有一些新的代码要求?我正在使用下面的代码。
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
我将 "en" 和 "ar" 作为语言参数传递。
当您设置新的 Locale
时,您应该重新启动 Activity
。您可以使用下一段代码执行它:
private void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
然后您的 changeLanguage()
方法将以下一种方式查看:
public void changeLanguage(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = context.getResources().getConfiguration();
config.setLocale(locale);
context.createConfigurationContext(config);
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
restartActivity();
}
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
context.getApplicationContext().getResources().updateConfiguration(config,
context.getApplicationContext().getResources().getDisplayMetrics());
这对我有用。
我在研究之前和之后都遇到了同样的问题,我找到了一个解决方案。 创建自定义语言感知上下文包装器并附加到 activity 的 attachBaseContext() 方法。
@SuppressWarnings("NewApi")
public static LanguageAwareContext newLanguageAwareContext(String targetLanguage, Context context) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
Locale newLocale = new Locale(targetLanguage);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList.setDefault(new LocaleList(newLocale));
context = context.createConfigurationContext(configuration);
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
} else {
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
return new LanguageAwareContext(context);
}
覆盖 attachBaseContext 方法并使用您自定义的上下文包装器进行更改
@Override
protected void attachBaseContext(Context newBase) {
newBase = LanguageAwareContext.newLanguageAwareContext("en",newBase);
super.attachBaseContext(newBase);
}
是的,Android Oreo 存在语言更改问题(阿拉伯语、乌尔都语和希伯来语的字符串和布局方向), 我找到了解决方案这将帮助你解决这个问题
在LanguageContextWrapper.javaclass
public static ContextWrapper wrap(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(locale);
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
configuration.setLayoutDirection(locale);
context.createConfigurationContext(configuration);
} else {
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return new LanguageContextWrapper(context);
}
在activityclass重写下面的方法
override fun attachBaseContext(base: Context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
super.attachBaseContext(LanguageContextWrapper.wrap(base, Locale.getDefault().language))
} else {
super.attachBaseContext(base)
}
}
更改语言后我们必须重新创建activity,我们需要编写以下代码
fun recreateActivity(){
if (Build.VERSION.SDK_INT in 26..27) {
if (Locale.getDefault().language == "ar" ||
Locale.getDefault().language == "iw" ||
Locale.getDefault().language == "ur")
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL
else
window.decorView.layoutDirection = View.LAYOUT_DIRECTION_LTR
}
recreate()
}
我最近在 Oreo
上遇到了与布局方向相关的问题。正在更新资源,但布局方向未更改。
下面的代码对我有用。
public static void setRTLSupportIfRequired(AppCompatActivity activity) {
if(getLanguageFromPrefs(activity).equals("ar")) {
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}else{
activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
}
注意:之前我使用的是 View.LAYOUT_DIRECTION_LOCALE
,但它不适用于 Oreo