单击按钮切换语言

Ron*_*Ron 9 resources android localization

当我点击"sub_changelang"按钮时,它应该将程序语言更改为法语.我得到以下代码来更改区域设置,但我不知道如何刷新/ pdate应用程序将语言更改为法语.

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; 
    } 
});
Run Code Online (Sandbox Code Playgroud)

它不起作用.我该如何解决?我试着添加:

MainActivity.this.getResources().updateConfiguration(config, MainActivity.this.getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我也尝试过:

getBaseContext().getResources().updateConfiguration(config,
                          getBaseContext().getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)

它也没用.

android:configChanges="locale"
Run Code Online (Sandbox Code Playgroud)

在application-> activity下的AndroidMainfest.xml中设置

Viv*_*ava 16

我正在使用此代码来设置区域设置

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);
Run Code Online (Sandbox Code Playgroud)

这里的上下文是应用程序Base Context. 也请尝试"fr"而不是"fr_FR",因为我正在为阿拉伯语语言环境工作并且工作正常.

更改区域设置后需要重新启动Activity.


And*_*diM 6

您可以使用activity.this.recreate().但它将支持API级别11.