点击按钮改变语言法国 [英] button click change language to france
问题描述
当我点击sub_changelang按钮,就应该更改程序语言法国为例。我得到了以下code键更改语言环境,但我不知道如何刷新/更新应用程序,所以它会在法国的显示。
When I click on "sub_changelang" button, it should change the program language to france for example. I got the following code to change the locale but I have no idea how to refresh / update the app so it will display in france.
Button cl = (Button) findViewById(R.id.sub_changelang);
cl.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Locale locale = new Locale("fr_FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
}
});
它不工作。我该如何解决?
我试图添加
it doesnt work. how can I fix it?
I tried to add
MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());
但它没有工作。
我也试过
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
和它没有工作,要么。
android:configChanges="locale"
根据应用程序设置的AndroidMainfest.xml内 - >活动
is set inside the AndroidMainfest.xml under application -> activity
推荐答案
我用这code设置区域
I am using this code to set locale
String languageToLoad = "fr_FR";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());
Intent intent = new Intent(XYZ.this, XYZ.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
在这里上下文是应用基本上下文。
另外,请尝试FR,而不是fr_FR目录,因为我工作的阿拉伯语语言环境和其工作的罚款。
here context is application Base Context. Please also try "fr" instead of "fr_FR" because I am working for Arabic locale and its working fine.
您需要更改区域设置后重新启动活动。
You need to restart your Activity after changing locale.
这篇关于点击按钮改变语言法国的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!