WebView 中的 Android +1 按钮
我尝试使用他们描述的方法将 Google 的 +1 按钮放入 WebView 中。我已按如下方式初始化 WebView:
final WebView web = (WebView)findViewById(R.id.webView);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setSavePassword(false);
web.getSettings().setBuiltInZoomControls(false);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setHorizontalScrollBarEnabled(false);
web.setBackgroundColor(0xff2e2e2e);
web.loadDataWithBaseURL(null, htmlCodeGoesHere, "text/html", "utf-8", null);
和 html 代码:
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone href="http://stackoverflow.com"></g:plusone>
问题是...按钮根本不显示。
我该如何修复它?顺便说一句 - 我还希望按钮启动一个新窗口,而不是使用 WebView。有一个简单的解决方案吗?
谢谢
I've tried putting Google's +1 button in WebView using the methods they describe. I've initialized the WebView as follows:
final WebView web = (WebView)findViewById(R.id.webView);
web.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setSavePassword(false);
web.getSettings().setBuiltInZoomControls(false);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setHorizontalScrollBarEnabled(false);
web.setBackgroundColor(0xff2e2e2e);
web.loadDataWithBaseURL(null, htmlCodeGoesHere, "text/html", "utf-8", null);
And the html code:
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone href="http://stackoverflow.com"></g:plusone>
The problem is... the button doesn't display at all.
How do I fix it? By the way - I also want the button to launch a new window instead using the WebView. Is there a simple solution?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在WebView的权限系统上。本地文件中的脚本访问外部资源时出现问题。解决方案是让WebView认为本地代码是从外部网站加载的。
该按钮将会出现,但不幸的是它在 WebView 中不能很好地工作。
The problem lies in permissions system in WebView. Scripts in local files have problems accessing external resources. The solution is to make WebView think local code was loaded from external website.
The button will appear, but unfortunetely it doesn't work well in WebView.
我对 WebView 不太有经验,但事实上按钮根本不显示,听起来这可能是您的 layout/main.xml 文件中的问题。你看过这个了吗?
另外,对于启动新窗口的按钮,我认为可以附加一个 setOnClickListener,一旦完成,只需将其视为按钮,然后打开一个新窗口。我希望这是可能的。
I am not too experienced with WebView, but the fact the the button doesn't show up at all, sounds like it could be an issue in your layout/main.xml file. Have you taken a look at this yet?
Also, for the button to launch a new window, I think it is possible to attach an setOnClickListener, once that is done just treat it as a button, and open a new window. I hope that is possible.