我正在研发一款支持两种不同语言的应用程序 -
以下是代码片段 -
问题是 - 如果我切换到阿拉伯语,布局方向会改变为RTL,这是可以预料的。然后我再次将语言切换回英语。但是这次,布局方向没有更新。它仍然是RTL,但应该是LTR。此时,如果我将语言更改为阿拉伯语,则布局方向会变为LTR,这又是错误的。我在清单文件的应用程序标记中添加了以下属性 -
- 英文
- 阿拉伯语
以下是代码片段 -
public static void setLocale(String languageCode, Context ctx) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= VERSION_CODES.N) {
updateResourcesLocale(ctx, locale);
} else {
updateResourcesLocaleLegacy(ctx, locale);
}
}
private static void updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
context.getApplicationContext().createConfigurationContext(configuration);
}
private static void updateResourcesLocaleLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
在应用程序的每个活动中,我已经重写了attachBaseContext方法。 我在这里使用了以下逻辑 -
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(ContextWrapper.onAttach(base, LanguageUtil.getSelectedLanguageFromSharedPreferences(base)));
}
这是实用类 -public class ContextWrapper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context, String defaultLanguage) {
persist(context, defaultLanguage);
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
private static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
问题是 - 如果我切换到阿拉伯语,布局方向会改变为RTL,这是可以预料的。然后我再次将语言切换回英语。但是这次,布局方向没有更新。它仍然是RTL,但应该是LTR。此时,如果我将语言更改为阿拉伯语,则布局方向会变为LTR,这又是错误的。我在清单文件的应用程序标记中添加了以下属性 -
android:supportsRtl="true"
另外,我已将所有左对齐改成了 start,右对齐改为 end。
在调试过程中观察到布局方向已正确保存。但是它在用户界面中没有反映出来。
我的minSdkVersion 19 & targetSdkVersion 27
我被卡在这个问题上,无法解决。有我漏掉的东西吗?