我发现非常奇怪的错误只能在Android N设备上重现.
在浏览我的应用程序时,有可能改变语言.这是改变它的代码.
public void update(Locale locale) {
Locale.setDefault(locale);
Configuration configuration = res.getConfiguration();
if (BuildUtils.isAtLeast24Api()) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
configuration.setLocale(locale);
} else if (BuildUtils.isAtLeast17Api()){
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)
这段代码非常适合我的游览活动(通过recreate()
调用),但在所有下一个活动中,所有String资源都是错误的.屏幕旋转修复它.我该怎么办这个问题?我应该以不同方式更改Android N的区域设置,还是只是系统错误?
PS这是我发现的.首次启动MainActivity(在我的巡演之后)Locale.getDefault()
是正确的,但资源是错误的.但在其他活动中,它给了我错误的Locale和来自此语言环境的错误资源.旋转屏幕(或可能是其他一些配置更改)Locale.getDefault()
是正确的.
我有一个多语言的应用程序与主要语言英语和第二语言阿拉伯语.
我打电话setLocale()
的onCreate()
每一个Activity
在我的应用程序:
public static void setLocale(Locale locale){
Locale.setDefault(locale);
Context context = MyApplication.getInstance();
final Resources resources = context.getResources();
final Configuration config = resources.getConfiguration();
config.setLocale(locale);
context.getResources().updateConfiguration(config,
resources.getDisplayMetrics());
}
Run Code Online (Sandbox Code Playgroud)
其中locale
一个是以下之一:
在调用之前 super.onCreate(savedInstanceState)
调用上述方法.
如文档中所述,
android:supportsRtl="true"
在清单中添加了.left
和right
属性start
,并end
分别.res\values-ar\strings
文件夹中,并将可绘制资源放在res\drawable-ar
文件夹中(对于其他资源也是如此).以上设置正常.改变后的Locale
到ar-AE
,阿拉伯文字和资源都正确地显示在我的活动.
但是,对于版本8.0及更高版本的所有Android设备,资源和布局方向都存在问题.
在版本低于8.0的设备上,RTL屏幕正确如下所示:
在所有8.0+的设备上,同样的屏幕看起来像这样:
这是错的.
事实证明,方向和资源都显示不正确.
这里有两个问题:
Locale
在应用配置中似乎没有更新正确的内容. …