gpt4 book ai didi

android - 如何更改整个 android 应用程序的语言?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:08:09 24 4
gpt4 key购买 nike

我想编写一个可以显示英文或中文的应用程序。我已经准备了2个string.xml,即value/strings.xml和value-zh-rHK/strings.xml。但我不知道如何通过 android 的 ListPreference 更改语言。

xml/preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<SwitchPreference
android:key="pref_nightmode"
android:title="@string/nightmode"
android:defaultValue="false">
</SwitchPreference>

<ListPreference
android:key="pref_lang"
android:title="@string/lang"
android:dialogTitle="Choose Language"
android:entries="@array/lang"
android:entryValues="@array/lang_value"
android:defaultValue="@string/lang_default">
</ListPreference>

和 Preferences.java
public class Preferences extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(drawer_menu[5]);
getFragmentManager().beginTransaction().replace(R.id.content_frame, new MainPreferenceFragment()).commit();
}

public static class MainPreferenceFragment extends PreferenceFragment {
String locale;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceManager pm = getPreferenceManager();
ListPreference lang = (ListPreference) pm.findPreference("pref_lang");
if(lang.getValue().equals("English")) {
locale = "en_US";
} else {
locale = "zh_HK";
}
}
}

该 Activity 扩展了 BaseActivity 因为我在那里有一个抽屉菜单。

最佳答案

您只能更改应用程序的语言设置。使用 Locale 类并更新默认配置。语言更改仅在 Activity 重新启动或重新启动应用程序时应用。使用以下语言环境类

public class LocaleUtils {

private static Locale sLocale;

public static void setLocale(Locale locale) {
sLocale = locale;
if(sLocale != null) {
Locale.setDefault(sLocale);
}
}

public static void updateConfig(ContextThemeWrapper wrapper) {
if(sLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration configuration = new Configuration();
configuration.setLocale(sLocale);
wrapper.applyOverrideConfiguration(configuration);
}
}

public static void updateConfig(Application app, Configuration configuration) {
if(sLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Wrapping the configuration to avoid Activity endless loop
Configuration config = new Configuration(configuration);
config.locale = sLocale;
Resources res = app.getBaseContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
}

应用类:
public class App extends Application {
public void onCreate(){
super.onCreate();
// get user preferred language set locale accordingly new locale(language,country)
LocaleUtils.setLocale(new Locale("iw"));
LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.updateConfig(this, newConfig);
}
}

基础 Activity :
public class BaseActivity extends Activity {
public BaseActivity() {
LocaleUtils.updateConfig(this);
}
}

以上答案来自: Changing Locale within the app itself

安卓文档: http://developer.android.com/reference/java/util/Locale.html

关于android - 如何更改整个 android 应用程序的语言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com