【问题标题】:How to change iframe size in Android WebView?如何在 Android WebView 中更改 iframe 大小?
【发布时间】:2014-07-29 11:16:28
【问题描述】:

我创建了一个网页并将其加载到 android WebView 中。

HTML 源代码:

<html>
    <head>
        <script type="text/javascript">
            function changeIframeSize(height, width){
                var iframe = document.getElementById("iframe1");
                iframe.height = height;
                iframe.width = width;
            }
        </script>
    </head>
    <body style="margin: 0; padding: 0"> 
        <iframe src="https://docs.google.com/forms/d/1_cAHFx_vODxMGGGwhrrXx0rGiv0vs-GLsPRMTJ0COgg/viewform?embedded=true" width="360" height="515" frameborder="0" marginheight="0" marginwidth="0" id="iframe1" style="margin: 0; padding: 0"> Loading...   
        </iframe>  
    </body>
</html>

我使用这种方法加载了这个页面:

webView.loadDataWithBaseURL("http://base", htmlSource, "text/html", "utf-8", "");

我想从 Java 代码更改 iframe 大小。我尝试使用 JavaScript 来做到这一点:

DisplayMetrics metrix = getResources().getDisplayMetrics();
int width = metrix.widthPixels;
int height = metrix.heightPixels;
float density = metrix.density;

String func = "javascript:changeIframeSize(" +
        (int)(height / density - (75 + (hasAdView() ? 50 : 0))) +
        ", " +
        (int)(width / density) +
        ")";

webView.loadUrl(func);

但它不起作用。有人可以帮帮我吗?

【问题讨论】:

    标签: javascript android html iframe webview


    【解决方案1】:

    第一次在loadUrl之前试试:

    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled( true);
    

    【讨论】:

    • 天哪...我怎么能忘记...谢谢
    猜你喜欢
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多