我想以編程方式更改語言。如何以編程方式更改語言?
所以我建立了兩個xml文件。
values-it
-->string.xml
values-en
-->string.xml
這是MainActivity代碼改變整個應用程序的語言:
//意大利
Resources res = getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("it");
res.updateConfiguration(conf, dm);
// ENGLISH
Resources res2 = getApplicationContext().getResources();
DisplayMetrics dm2 = res2.getDisplayMetrics();
android.content.res.Configuration conf2 = res2.getConfiguration();
conf2.locale = new Locale("en");
res2.updateConfiguration(conf2, dm2);
現在,如果我設置英語(例如)代碼執行時沒有錯誤,但標籤不會更改其文本。
如果我更改設備的方向,標籤會正確更改其文本。
現在如何修改我的代碼以自動刷新標籤?
嘗試在onResume()中調用您的代碼。 – Mehta
嗨,在這裏你是一個鏈接,告訴你如何重新加載更改而不關閉活動:http://stackoverflow.com/questions/2644377/changing-locale-force-activity-to-reload-resources –
可能的重複[如何刷新活動後改變語言(區域設置)內應用程序](http://stackoverflow.com/questions/8049207/how-to-refresh-activity-after-changing-language-locale-inside-application) –