WebView 语言错误 Android N [英] WebView language error Android N
问题描述
我知道 图片,我在运行 7.1.1 的 Pixel 设备上设置了两种语言.现在,我正在尝试在我的应用程序的 WebView 中加载以下网址.
"https://accounts.google.com/ServiceLogin?"
我注意到页面加载的是中文而不是英语,这是我当前的语言环境.我尝试在我的 Chrome 桌面上打开上面的链接(我使用与手机相同的帐户登录),它也加载了中文!我转到 Chrome 的设置,发现它们的语言首选项已保存到我的帐户中,因为它们显示在 也是.
我确实尝试在我的应用程序中的 setContentView()
之前设置语言环境,如下所示:
public static void setLocale(Locale locale){Locale.setDefault(locale);配置配置=新配置();config.setLocale(locale);上下文上下文 = MyApplication.getInstance();context.getResources().updateConfiguration(config,context.getResources().getDisplayMetrics());}
但徒劳无功.如果我输入日志语句,它会打印正确的 Locale
,即 en-US
.WebView
仍然加载中文页面.关于如何修复它的任何想法?
问题受Android N中的webview影响.
- 在第一次启动 webview 时,它会将区域设置重置为默认值.
- 如果您随后旋转手机 - 语言环境会正确设置回自定义语言环境.
- 如果您随后再次启动 webview - 区域设置保持正确.
要解决此问题,您必须在页面加载后切换回您选择的语言.
您还可以查看 Activity/Fragment 生命周期何时切换语言.
来源:
https://issuetracker.google.com/issues/37113860https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chineseAndroid 4.1+ 中区域设置更改后活动闪烁一个>
I am aware of this question asked before about the WebView being broken in Android N especially with localization. I have another problem which I cannot figure out how to fix.
As shown in pic, I have two languages set on my Pixel device running 7.1.1. Now, I'm trying to load the following url in a WebView in my app.
"https://accounts.google.com/ServiceLogin?<my-params>"
What I notice is that the page loads in Chinese instead of English which is my current Locale language. I tried opening the above link on my Chrome desktop (where I am signed in with the same account as my phone) and it loads in Chinese too! I went to Chrome's settings to find that they language preferences are saved to my account because they show up in too.
I did try setting locale before setContentView()
in my app like this:
public static void setLocale(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
Context context = MyApplication.getInstance();
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
but in vain. If I put a log statement, it prints the correct Locale
which is en-US
. The WebView
still loads the page in Chinese though. Any thoughts on how can I fix it?
The issue is affected by the webview in the Android N.
- On the first launching of the webview, it resets the locale to default.
- If you then rotate the phone - the locale gets correctly set back to the custom locale.
- If you then launch the webview again - the locale stays correct.
To fix this, you have to switch back to your chosen language after the page was loaded.
You may also check on the Activity/Fragment life cycle when to switch language.
Sources:
https://issuetracker.google.com/issues/37113860 https://gist.github.com/amake/0ac7724681ac1c178c6f95a5b09f03ce#new-locales-vs-old-locales-chinese Activity is blinking after locale change in Android 4.1+
这篇关于WebView 语言错误 Android N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!