我有一个多语言应用程序,主要语言为英语,次要语言为阿拉伯语。
我正在打电话setLocale()
in the 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());
}
where locale
是以下之一:
上面的方法被称为before super.onCreate(savedInstanceState)
被叫。
如中所述文档 https://developer.android.com/about/versions/android-4.2.html#RTL,
- 我已经添加了
android:supportsRtl="true"
在清单中。
- 我已经更改了所有 xml 属性
left
and right
属性为start
and end
分别。
- 我已将阿拉伯语字符串放入
res\values-ar\strings
文件夹和可绘制资源res\drawable-ar
文件夹(其他资源也类似)。
上述设置工作正常。更改后Locale
to ar-AE
、阿拉伯文本和资源在我的活动中正确显示。
但是,所有版本的Android设备都存在资源和布局方向的问题8.0以上。
在版本低于8.0的设备上,RTL屏幕正确地看起来像这样:
在所有 8.0+ 的设备上,相同的屏幕看起来像这样:
这是wrong.
事实证明,方向和资源都在得到
显示不正确。
有two这里的问题:
- 正确的
Locale
似乎没有在应用程序配置中更新。
- 文本和绘图的方向与应有的方向相反。
关于方向,有一种奇怪的方法,称为setLayoutDirection() https://developer.android.com/reference/android/content/res/Configuration.html#setLayoutDirection(java.util.Locale)存在我以前没有注意到的。
我想知道这个问题是什么,为什么它会在奥利奥中发生以及解决方案是什么。请对此提供帮助/评论。
EDIT:
根据API差异
报告 https://developer.android.com/sdk/api_diff/25/changes, the
updateConfiguration() https://developer.android.com/reference/android/content/res/Resources#updateConfiguration(android.content.res.Configuration,%20android.util.DisplayMetrics)该方法确实在 Android 7.1(API 级别 25)中已被弃用。
另外,找到了所有与此相关的帖子。按重要性排序:
1. Android N 以编程方式更改语言 https://stackoverflow.com/q/39705739.
2. Android context.getResources.updateConfiguration() 已弃用 https://stackoverflow.com/q/40221711.
3. 如何更改 Android O / Oreo / api 26 应用程序语言 https://stackoverflow.com/q/47165311.
4. API 24 及更高版本中区域设置更改时出现 Android RTL 问题 https://stackoverflow.com/q/44515264
5. 以编程方式更改语言(Android N 7.0 - API 24) https://stackoverflow.com/q/38997356.
6. Android N - 在运行时更改区域设置 https://stackoverflow.com/q/44554626.
7. Android Oreo 中的 RTL 布局错误 https://stackoverflow.com/q/46296202.