以下是更改语言的代码
eng.setOnClickListener(setChangeLangListener("en"));
chi.setOnClickListener(setChangeLangListener("zh"));
public OnClickListener setChangeLangListener(final String lang) {
OnClickListener changeLangListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Configuration config = new Configuration(getResources()
.getConfiguration());
if (lang.equals("en")) {
config.locale = Locale.ENGLISH;
chi.setTextColor(oldColor);
eng.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
} else {
config.locale = Locale.TRADITIONAL_CHINESE;
eng.setTextColor(oldColor);
chi.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
}
getResources().updateConfiguration(config,
getResources().getDisplayMetrics());
}
};
return changeLangListener;
}
问题是,当我更改区域设置时,图像视图不刷新以反映更改,我使用tabhost并且更改语言按钮位于片段内,因此我必须更改actvitiy + inside片段。怎么解决?感谢。
tabhost
tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);
tabHost.addTab(
tabHost.newTabSpec("Home").setIndicator("",
res.getDrawable(R.drawable.btn_about)), Home.class,
null);
tabHost.addTab(
tabHost.newTabSpec("About").setIndicator("",
res.getDrawable(R.drawable.btn_about)), About.class,
null);
tabHost.addTab(
tabHost.newTabSpec("Check").setIndicator("",
res.getDrawable(R.drawable.btn_check)), Selfie.class,
null);
tabHost.addTab(
tabHost.newTabSpec("Gallery").setIndicator("",
res.getDrawable(R.drawable.btn_gallery)),
PhotoGallery.class, null);
tabHost.addTab(
tabHost.newTabSpec("LeaderBoard").setIndicator("",
res.getDrawable(R.drawable.btn_board)),
LeaderBoard.class, null);
tabHost.addTab(
tabHost.newTabSpec("Events").setIndicator("",
res.getDrawable(R.drawable.btn_events)), Events.class,
null);
tabHost.getTabWidget().setDividerDrawable(null);
setTabBg(tabHost);
片段
rootView = inflater.inflate(R.layout.home, container, false);
loginBtn = (ImageView) rootView.findViewById(R.id.home_connectFB);
eng = (TextView) rootView.findViewById(R.id.btn_eng);
chi = (TextView) rootView.findViewById(R.id.btn_chi);
答案 0 :(得分:0)
您的View.OnClickListener
错了。或者至少我读它的方式。当你按下eng或chi时,你想改变一些东西。 Imo你应该将它作为成员类内联,或者让你的活动实现OnClickListenerm,并为TextView设置相同的内容。例如:
OnClickListener changeLangListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Configuration config = new Configuration(getResources()
.getConfiguration());
if (arg0.getId() = R.id.btn_eng) {
config.locale = Locale.ENGLISH;
chi.setTextColor(oldColor);
eng.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
} else if (arg0.getId() = R.id.btn_chi) {
config.locale = Locale.TRADITIONAL_CHINESE;
eng.setTextColor(oldColor);
chi.setTextColor(getActivity().getResources().getColor(
android.R.color.white));
}
getResources().updateConfiguration(config,
getResources().getDisplayMetrics());
}
};
eng.setOnClickListener(changeLangListener);
chi.setOnClickListener(changeLangListener);
答案 1 :(得分:0)
public void setLocale(String lang){
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
onConfigurationChanged(conf);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
iv.setImageDrawable(getResources().getDrawable(R.drawable.keyboard));
greet.setText(R.string.greet);
textView1.setText(R.string.langselection);
super.onConfigurationChanged(newConfig);
}