Amr*_*rut 3 android localization android-layout android-configchanges
我正在开发一个支持两种不同语言的应用程序 -
用户可以随时切换到任何语言,并根据我正在更改应用程序语言的选择。
下面提到的是代码片段 -
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());
}
Run Code Online (Sandbox Code Playgroud)
同样在应用程序的每个活动中,我都覆盖了attachBaseContext方法。我在其中使用了以下逻辑-
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(ContextWrapper.onAttach(base, LanguageUtil.getSelectedLanguageFromSharedPreferences(base)));
}
Run Code Online (Sandbox Code Playgroud)
这是实用程序类 -
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;
}
Run Code Online (Sandbox Code Playgroud)
问题是 - 如果我切换到阿拉伯语,布局方向将更改为 RTL,这是意料之中的。然后我又将我的语言切换为英语。但是这一次,布局方向没有更新。它仍然是 RTL,但它应该是 LTR。此时,如果我将语言更改为阿拉伯语,布局方向将转到 LTR,这又是错误的。我在清单文件的应用程序标签中添加了以下属性-
android:supportsRtl="true"
Run Code Online (Sandbox Code Playgroud)
另外,我已经用结束的开始和右对齐替换了所有左对齐。
在调试时观察到布局方向被正确保存。但是它没有反映在用户界面中。
我的 minSdkVersion 19 & targetSdkVersion 27
我被困在这个问题上,无法解决这个问题。有什么我在这里想念的吗?
Amr*_*rut 15
最后,经过大量的反复试验,我通过在调用setContentView 之前在 onCreate 方法的所有活动中添加以下代码来解决此问题。
if (BuildConfig.FLAVOR_country.equals("saudi") && "ar".equals(LanguageUtil.getSelectedLanguageFromSharedPreferences(this))) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
} else {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2698 次 |
| 最近记录: |