我的应用程序广泛使用片段。它应该支持3种语言 - 英语、阿拉伯语和库尔德语。主屏幕由一个导航抽屉组成,其中包含一个选项菜单,其中之一是语言,它打开一个语言选择对话框。语言选择器有3个按钮 - 英语、库尔德语和阿拉伯语。点击按钮会更改语言,如下所示 -
我已将以下代码添加到onConfigurationChange中 -
private void setLocale(String code){
Configuration config=new Configuration();
Locale locale = null;
locale=new Locale(code);
Locale.setDefault(locale);
config.locale=locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
dismiss();
}
我已将以下代码添加到onConfigurationChange中 -
FragmentManager fragmentManager = getSupportFragmentManager();
int count=fragmentManager.getBackStackEntryCount();
BackStackEntry latestEntry=(BackStackEntry) fragmentManager.getBackStackEntryAt(count-1);
String str=latestEntry.getName();
Fragment fragment=fragmentManager.findFragmentByTag(str);
if(fragment==null){
return;
}
FragmentTransaction fragTransaction = fragmentManager.beginTransaction();
fragTransaction.detach(fragment);
fragTransaction.attach(fragment);
fragTransaction.commit();
我已经将以下内容添加到了Android清单文件中 -
android:configChanges="locale"
但是似乎没有任何进展。我还尝试了刷新Fragment的视图,但运气不太好。非常感谢您的帮助。