Android +1 button in WebView

怎甘沉沦 提交于 2019-12-19 10:17:13

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7813106/android-1-button-in-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!