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

有帮助吗?

解决方案

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.

web.loadDataWithBaseURL("http://fake.com", htmlCodeGoesHere, "text/html", "utf-8", null);

The button will appear, but unfortunetely it doesn't work well in WebView.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top