2011-06-24 103 views
30

可能重复:
Change language programatically in Android我怎样才能改变我的应用程序的语言

我是新来的Android。在我的应用程序中,用户可以选择三种语言的语言。根据用户选择的语言,应该更改整个应用程序的语言。我怎样才能做到这一点?

+0

使用此改变语言编程: 'code' Locale locale = new Locale(“en_US”); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; context.getApplicationContext()。getResources()。updateConfiguration(config,null); setContentView(R.layout.activity_main); 'code' –

回答

6

您可以设置区域设置。

Resources res = context.getResources(); 
    // Change locale settings in the app. 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    android.content.res.Configuration conf = res.getConfiguration(); 
    conf.locale = new Locale(language_code.toLowerCase()); 
    res.updateConfiguration(conf, dm); 

如果您有语言特定的内容 - 您可以更改该设置的基础。 更多的细节,你可以看到Locale this also

49

使用此以编程方式更改语言:

Locale locale = new Locale("en_US"); 
Locale.setDefault(locale); 
Configuration config = new Configuration(); 
config.locale = locale; 
context.getApplicationContext().getResources().updateConfiguration(config, null); 

编写语言的国家代码代替"en_US"为任何你想要的语言。例如,对于日语,ja_JP;阿拉伯文,ar。检查this link的列表。

,并在res/values-ja一个文件夹日语或res/values-ar阿拉伯语..

并做好string.xml文件,并把您是否想在布局什么语言。这将获取从值文件夹的默认语言,否则如果你想手动,那么它会从你的外部文件夹values-ar等取

阿拉伯语的res/values-ar一个例子:

<?xml version="1.0" encoding="UTF-8"?> 
    <resources> 
    <string name="label">حسب</string> 
    <string name="name">بحث</string> 
    <string name="search">بحث :</string> 
</resource> 
+0

在哪里调用你的代码!? – MBH

+0

在需要的地方拨打它。 – Hulk

+1

会调用它一次足以将其应用于整个应用程序?或每一项活动? – MBH

相关问题