2016-05-12 95 views
0
  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(); 
} 
  • 我怎樣才能保存我的配置?如果我關閉了應用程序並再次打開,我想要最後的配置。
  • 回答

    0

    你可以試試這個解決方案: 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()); 
    
    0

    您可以使用共享偏好存儲所選的語言。每次你再次進行同樣的活動時,首先檢查共享偏好。這樣 Language switching inside app android

    我希望這將幫助你:)

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

    這爲我工作。您可以將「en」更改爲其他語言。我遇到了同樣的問題,但最終發現了這個代碼。希望它也能幫助你。當然,如果你想要或在啓動時,你可以用clicklistener把它放在一個按鈕中。這段代碼對我來說無處不在。祝你好運。

    相關問題