android - 区域设置未在发布包中更新
问题描述
语言环境在发布包中没有更新,但它在调试 apk 中工作。我正在使用 targetSdkVersion 29 和 androidx.appcompat:appcompat:1.3.0-alpha02
在 MainActivity
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.updateResources(base));
}
public class LocaleManager {
public static String TAG = "LocaleManager";
public static String LOCALE_HI = "hi_IN";
public static String LOCALE_EN = "en_US";
private static String lan_str;
private static String country_str;
public static void setlan(String str)
{
String val[] = str.split("_");
LocaleManager.lan_str = val[0];
LocaleManager.country_str = val[1];
}
public static String getlanguage()
{
return LocaleManager.lan_str+"_"+LocaleManager.country_str;
}
public static Context updateResources(Context context) {
if(lan_str == null || country_str == null)
return context;
Locale locale = new Locale(lan_str,country_str);
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
config.setLocale(locale);
config.locale = locale;
} else {
config.locale = locale;
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
context = context.createConfigurationContext(config);
} else {
res.updateConfiguration(config, res.getDisplayMetrics());
}
return context;
}
}
更改语言后,我正在重新启动 MainActivity,但语言环境未更新。
解决方案
最后我得到了定位的解决方案,在 app build.gradle 的 android 块中添加了这个
bundle {
language {
enableSplit = false
}
}
推荐阅读
- c++ - C++20 在 string/u8string 和 string_view/u8string_view 之间转换
- python - Unpickling 类会引发属性错误
- go - 编辑 Google 日历“发件人”和“组织者”
- javascript - Angular Dragula:拖放后如何在保存按钮上获取更新排序列表
- html - 从前端到后端的日期时间本地发送时间自动更改,并减去 2 小时
- ruby-on-rails - 未找到 Rails 资产清单文件(或备份文件)
- linux - 在 Ubuntu 20.04.1 LTS 上安装 GCC 4.6.2
- java - Navigable Map - 已排序的地图但未排序
- c# - 为什么他们在 GetWindowRect 函数中使用它
- python - 在 PyCharm 中调试时,Pytest/Fabric 不使用 SSH 代理