问题
I want to load an HTML <IFRAME>
inside an WebView
, but I don't know why, it is not able to do so.
I am using following code to load <IFRAME>
webView.loadData("<iframe src=\"http://www.google.com\"></iframe>", "text/html",
"utf-8");
Here's what I have tried.
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginsEnabled(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setPluginState(PluginState.ON);
I have mentioned the internet permission:
<uses-permission android:name="android.permission.INTERNET" />
I have also tried settings the WebViewClient
with shouldOverrideUrlLoading
always returning false.
But it just isn't working.
I have tried this with diffrent sites, i.e. sites other than, google.com.
I am testing this on, Samsung Nexus S running ICS 4.0.3
回答1:
This is how it worked out.
I noticed Log cat was throwing me
WebKit permission issue: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up
To fix this, I had to add android:hardwareAccelerated="true"
in <application>
tag of Manifest.
I was experiencing this on ICS and its found that the same issue will occur post Honeycomb devices.
Hope this will help some one out.
回答2:
Try with the below code:
webView.setInitialScale(1);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Log.e(SimpleBillsConstants.SIMPLE_BILLS, width + "-" + height);
String data_html = "<!DOCTYPE html><html> <head> <meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"target-densitydpi=high-dpi\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" media=\"screen and (-webkit-device-pixel-ratio:1.5)\" href=\"hdpi.css\" /></head> <body style=\"background:black;margin:0 0 0 0; padding:0 0 0 0;\"> <iframe style=\"background:black;\" width=' "+width+"' height='"+height+"' src=\""+VIDEO_URL+"\" frameborder=\"0\"></iframe> </body> </html> ";
webView.loadDataWithBaseURL("http://vimeo.com", data_html, "text/html", "UTF-8", null);
回答3:
following hack worked for me for loading iframes in webview.Hope some one still might find it useful
String webContent="your data to be loaded in webview"
if(webContent.contains("iframe")){
Matcher matcher = Pattern.compile("src=\"([^\"]+)\"").matcher(webContent);
matcher.find();
String src = matcher.group(1);
webContent=src;
try {
URL myURL = new URL(src);
webView.loadUrl(src);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}else {
webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;}</style>" + webContent, "text/html", "UTF-8", null);}
}
}
来源:https://stackoverflow.com/questions/8955228/webview-with-an-iframe-android