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

时间:2016-04-28 17:51:13

标签: android locale preferences

我想编写一个可以显示英文或中文的应用程序。我已经准备了2个string.xml,它是值/ 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";
        }
    }
}

活动扩展了BaseActivity,因为我有一个抽屉菜单。

1 个答案:

答案 0 :(得分:10)

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

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);
    }
}

BaseActivity:

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

以上回答摘自:Changing Locale within the app itself

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