【问题标题】:WebView with an IFRAME android带有 IFRAME android 的 WebView
【发布时间】:2012-01-21 17:55:28
【问题描述】:

我想在WebView 中加载 HTML <IFRAME>,但我不知道为什么,它不能这样做。

我正在使用以下代码加载<IFRAME>

webView.loadData("<iframe src=\"http://www.google.com\"></iframe>", "text/html",
                "utf-8");

这是我尝试过的。

WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setPluginsEnabled(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setPluginState(PluginState.ON);

我已经提到了互联网权限:

<uses-permission android:name="android.permission.INTERNET" />

我还尝试将WebViewClient 设置为shouldOverrideUrlLoading 始终返回false。

但它只是不起作用。

我已经在不同的网站上尝试过这个,即 google.com 以外的网站。

我正在测试这个,三星 Nexus S 运行 ICS 4.0.3

【问题讨论】:

    标签: android iframe webview load


    【解决方案1】:

    试试下面的代码:

    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);
    

    【讨论】:

    • 只是一个问题。如果您执行setPluginState(WebSettings.PluginState.ON); 并且在您还执行setPluginState(WebSettings.PluginState.ON_DEMAND); 之后,后者将覆盖前者,对吗?或者你设置一个或另一个而不是两者。
    • developer.android.com/reference/android/webkit/… 我认为这将有助于您的问题。但正如你所说,我们可以使用任何一个。
    • 我认为这个答案应该被标记为正确答案,因为它对我来说效果很好
    【解决方案2】:

    结果就是这样。

    我注意到原木猫在扔我

    WebKit 权限问题:EventHub.removeMessages(int what = 107) 是 在设置 WebViewCore 之前不支持

    要解决这个问题,我必须在 Manifest 的 &lt;application&gt; 标签中添加 android:hardwareAccelerated="true"

    我在 ICS 上遇到过这个问题,发现在 Honeycomb 设备之后也会出现同样的问题。

    希望这能帮助一些人。

    【讨论】:

    • 嗨,这个 android:hardwareAccelerated="true" 是什么意思?
    • @Shardul:见屏幕截图:i.imgur.com/OyCxlwh.png,hardwareAccelerated=true 已设置,但 iframe 无法加载 google.com 页面
    • 嗨,这个 android:hardwareAccelerated="true" 属性在应用程序标签中找不到
    • 如果 minSDK 或 targetSDK >= 14 则默认为 true,因此对我没有解决问题
    【解决方案3】:

    以下 hack 对我在 webview 中加载 iframe 有用。希望有人仍然会觉得它有用

        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);}
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2018-08-24
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 2012-10-08
      相关资源
      最近更新 更多