以编程方式更改语言

问题描述:

  1. 当我选择另一种语言时,如何在所有活动中更改语言?如果我回到以前打开的活动,它仍然具有相同的配置。如何刷新该活动呢?

我使用这种方法:以编程方式更改语言

public void setLocale(String lang) { 
    Locale myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    android.content.res.Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, Language.class); 
    startActivity(refresh); 
    finish(); 
} 
  1. 我怎样才能保存我的配置?如果我关闭了应用程序并再次打开,我想要最后的配置。
开始=>

你可以试试这个解决方案: Set Locale programmatically

Locale locale = new Locale(lang); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 

您可以使用共享偏好存储所选的语言。每次你再次进行同样的活动时,首先检查共享偏好。这样 Language switching inside app android

我希望这将帮助你:)

myLocale = new Locale("en"); 
     Resources res = getResources(); 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     Configuration conf = res.getConfiguration(); 
     conf.locale = myLocale; 
     res.updateConfiguration(conf, dm); 

这为我工作。您可以将“en”更改为其他语言。我遇到了同样的问题,但最终发现了这个代码。希望它也能帮助你。当然,如果你想要或在启动时,你可以用clicklistener把它放在一个按钮中。这段代码对我来说无处不在。祝你好运。