在我的Android应用程序中,我想更改语言表单片段。我的应用程序包含MainActivity.java。此活动包含导航抽屉和片段。当我从片段中更改我的语言环境时,语言会在导航抽屉中的每个位置都更改。我正在使用代码
private String setLocal() {
Locale locale;
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
prefs = getActivity().getSharedPreferences("Language", 0);
String restoredText = prefs.getString("language", "");
if (restoredText.equalsIgnoreCase("fr_FR"))
locale = new Locale("fr");
else
locale = new Locale("en");
Locale.setDefault(locale);
conf = new Configuration();
res.updateConfiguration(conf, dm);
return restoredText;
}
改变语言后我得到了这个
<string-array name="nav_drawer_titles">
<item>Accueil</item>
<item>Profil</item>
<item>courses</item>
<item>Tarification</item>
<item>Configuration</item>
<item>Envoyez à un ami</item>
<item>Facture</item>
<item>Position du Chaufeur</item>
<item>Aide</item>
<item>Déconnexion</item>
</string-array>
此处来自值文件夹
<!-- Nav Drawer Menu Items -->
<string-array name="nav_drawer_titles">
<item>Home</item>
<item>Profile</item>
<item>Ride Log</item>
<item>Invoice</item>
<item>Track Chauffeur</item>
<item>Language</item>
<item>Refer To Friend</item>
<item>Help</item>
<item>Sign Out</item>
</string-array>
我的navigationdrawerfragnet是
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
title = getResources().getStringArray(R.array.nav_drawer_titles);
images = getResources().getStringArray(R.array.nav_drawer_icons);
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
selectedposition = new int[] { mCurrentSelectedPosition };
drawerAdapter = new CustomDrawerAdapter(getActivity(), title, images,
selectedposition);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
mDrawerListView.setAdapter(drawerAdapter);
return mDrawerListView;
}
Im MainActivity.java
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onPrepareOptionsMenu(menu);
invalidateOptionsMenu();
setLocal();
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setLocal();
invalidateOptionsMenu();
}
答案 0 :(得分:3)
您应该在setContentView(int resId)
之前在抽屉活动中设置您的区域设置。
同样在onResume()
方法中,您可以使用API_LEVEL >= 11
重新调整您的活动
致电super.recreate()
以及11
以下,您可以设置onCreate(null)
。
答案 1 :(得分:0)
检查this
title = getResources().getStringArray(R.array.nav_drawer_titles);
images = getResources().getStringArray(R.array.nav_drawer_icons);
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
selectedposition = new int[] { mCurrentSelectedPosition };
drawerAdapter = new CustomDrawerAdapter(getActivity(), title, images,
selectedposition);
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
mDrawerListView.setAdapter(drawerAdapter);
您需要在setLocal方法上按下该行以使抽屉列表无效