如何从SettingsActivity更改应用程序的区域设置?
问题描述:
我想从设置中更改应用程序的语言。如何从SettingsActivity更改应用程序的区域设置?
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index]: null);
.............
这里,设置活动里面我们怎么能访问的配置和应用程序的语言环境来改变它的语言。
仅供参考,我已将值转换为字符串。
答
检查这个代码
public static final String ARABIC = "ar";
public static final String ENGLISH = "en";
public static void changeLoc(Activity context, int flagLang)
{
Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
if(flagLang == 0)
{
//english
conf.locale = new Locale(ENGLISH);
}
else
{
//arabic
conf.locale = new Locale(ARABIC);
}
res.updateConfiguration(conf, dm);
}
这是http://stackoverflow.com/questions/13181847/change-the-locale-at-runtime的副本? – k3b