【发布时间】:2018-08-31 14:00:08
【问题描述】:
我正在制作一个应用程序,我希望在其中有一个显示语言选择页面的页面。到目前为止,我已经包括了英语、印地语和马拉地语,英语设置为默认值。
我的问题是:
如何在所选语言中更改整个应用程序语言?
在我重新打开应用程序时选择语言后,它会提供以前选择的语言吗?
【问题讨论】:
标签: android android-layout android-fragments
我正在制作一个应用程序,我希望在其中有一个显示语言选择页面的页面。到目前为止,我已经包括了英语、印地语和马拉地语,英语设置为默认值。
我的问题是:
如何在所选语言中更改整个应用程序语言?
在我重新打开应用程序时选择语言后,它会提供以前选择的语言吗?
【问题讨论】:
标签: android android-layout android-fragments
将所有文本放入字符串文件中。为每种语言创建单独的字符串文件(Deutsch values-de/strings.xml,French values-fr/strings.xml) 并且您需要更改语言调用以下功能。为英文集"en"为另一集对应键
#Kotlin
val config = resources.configuration
val locale = Locale("en")
Locale.setDefault(locale)
config.locale = locale
resources.updateConfiguration(config, resources.displayMetrics)
#Android Java
Configuration config = getBaseContext().getResources().getConfiguration();
Locale locale = new Locale("en");
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
【讨论】:
String lang= "en";
public void changeLang(String lang) {
Configuration config = getBaseContext().getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
}
}
试试这个方法..这绝对行得通.. 当您选择首选语言时,在此方法中传递您选择的语言代码,这将更改整个应用程序的语言。
【讨论】: