我发现使用返回“昨天”或“2 小时前”等值的 android.text.format.DateUtils
相关 API 非常好 - 但我的应用程序不支持所有 Android 语言做。因此,我默认使用英语,但对于我不支持的每种语言,相关字符串都会显示在设备设置中。
例如,喜欢:
上次尝试:11 分钟。
对于我不支持的任何语言,我想让 API 调用默认为英语。但是,我看不到任何地方可以为 API 调用设置区域设置 - 我希望我只是在某处遗漏了它。
有没有办法只为 API 调用设置区域设置,而忽略设备设置?
最佳答案
这适用于 Android 7
void forceLocale(Locale locale) {
Configuration conf = getBaseContext().getResources().getConfiguration();
updateConfiguration(conf, locale);
getBaseContext().getResources().updateConfiguration(conf, getResources().getDisplayMetrics());
Configuration systemConf = Resources.getSystem().getConfiguration();
updateConfiguration(systemConf, locale);
Resources.getSystem().updateConfiguration(conf, getResources().getDisplayMetrics());
Locale.setDefault(locale);
}
void updateConfiguration(Configuration conf, Locale locale) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
conf.setLocale(locale);
}else {
//noinspection deprecation
conf.locale = locale;
}
}
关于android - 强制 Android DateUtils.getRelativeDateTimeString() 忽略设备区域设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6539195/