2014-10-29 101 views
-3

我有一個應用程序,我想用三種語言。現在,我創建了3個字符串資源與不同的語言,我的整個應用程序引用第1個。是否可以在事件觸發器上引用其他2?任何人都可以詳細解釋請。我聽說過locale對象,但我不是很確定它是如何工作的更改res/string programaticlly

+2

我中有你不明白本土化的概念還沒有感覺。請仔細閱讀文檔:http://developer.android.com/guide/topics/resources/localization.html – Blacklight 2014-10-29 13:32:45

回答

0

是的,你可以在event..or點擊事件在運行時爲此使用此代碼

private void changeLanguage(String lang) { 
    // TODO Auto-generated method stub 

    Configuration conf = getApplicationContext().getResources() 
      .getConfiguration(); 
    conf.setToDefaults(); 
    /// 
    if (conf.locale == null) 
     conf.locale = Locale.getDefault(); 
    ////// 
    if (lang.equalsIgnoreCase("english")) { 
     conf.locale = Locale.ENGLISH; 
    } else if (lang.equalsIgnoreCase("chinese")) { 
     conf.locale = Locale.TRADITIONAL_CHINESE; 
    } 

    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    Resources resources = new Resources(
      getApplicationContext().getAssets(), metrics, conf); 
    /* get localized string */ 
    String str = resources.getString(R.string.back); 
    Log.i("mini", "Chinese:" + str); 

}