2016-11-30 135 views
3

地區語言變化,這是我的代碼: -Android的 - 在使用上的棒棒糖

SplashActivity.java

public class SplashActivity extends AppCompatActivity { 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_splash);`langData = "hi"` 
      setLocale(langData); 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 

        //setLocale(langData); 
        startActivity(new Intent(SplashActivity.this, MainActivity.class)); 
        finish(); 
       } 
      }, 3000); 
     } 



    public void setLocale(String lang) { 
     Resources res = this.getResources(); 
     // Change locale settings in the app. 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     android.content.res.Configuration conf = res.getConfiguration(); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      setSystemLocale(conf, new Locale(lang.toLowerCase())); 
     } else { 
      setSystemLocaleLegacy(conf, new Locale(lang.toLowerCase())); 
     } 
     res.updateConfiguration(conf, dm); 
     //startActivity(new Intent(this,MainActivity.class)); 
     //finish(); 
     //this.setContentView(R.layout.activity_main); 
    } 

    @SuppressWarnings("deprecation") 
    public Locale getSystemLocaleLegacy(Configuration config) { 
     return config.locale; 
    } 

    @TargetApi(Build.VERSION_CODES.N) 
    public Locale getSystemLocale(Configuration config) { 
     return config.getLocales().get(0); 
    } 

    @SuppressWarnings("deprecation") 
    public void setSystemLocaleLegacy(Configuration config, Locale locale) { 
     config.locale = locale; 
    } 

    @TargetApi(Build.VERSION_CODES.N) 
    public void setSystemLocale(Configuration config, Locale locale) { 
     config.setLocale(locale); 
    } 
} 

,當我在我的模擬器運行這段代碼 (安卓7.0阿比24),它改變了語言在我的MainActivity平穩沒有造成任何錯誤,但是當我在棒棒糖模擬器上嘗試它它並沒有改變語言請幫助,並提前致謝:-)

+0

你有沒有試過在這個問題的答案:http://stackoverflow.com/questions/27164579/locale-not-set-programatically-in-android-5-0-lollipop –

+0

是的,我已經嘗試過,但它仍然無法正常工作在我的情況 –

回答

5

只是忘了在那裏添加國家代碼new Locale(lang.toLowerCase(),"ISO- COUNTRY_CODE") 和應用程序運行良好。