Android 在 iframe 中嵌入视频未根据 webview 高度变化调整大小

2024-04-26

我正在尝试在 webview 中播放嵌入式 YouTube 视频。我的问题是当我旋转屏幕并调整 webview 高度时,iframe 不会改变其高度。

String embedSrc = "https://www.youtube.com/embed/8SeRU_ZPDkE";
String iframe = "<html><body style=\"margin: 0; padding: 0; background: #000;\"><iframe width=\"100%\" height=\" 100% \" src=\"" + embedSrc + "frameborder=\"0\" allowfullscreen style=\"background: #000;\"></iframe></body></html>";

webView.loadData(iframe, "text/html", "utf-8");

 WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);

        String userAgent = settings.getUserAgentString();
        if (userAgent != null) {
            userAgent = userAgent.replace("Android", "");
            settings.setUserAgentString(userAgent);
        }

//code to update webview height
webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, newHeight));


//layout
<LinearLayout
    android:id="@+id/root_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical"
    >

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:scrollbars="none"
        />

</LinearLayout>

我已经尝试过这个

webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);

但这段代码使 youtube Playback 控件变得非常小。


通过使用这个解决了 webview 中 iframe 的这个问题响应式 Iframe CSS 技巧 https://www.smashingmagazine.com/2014/02/making-embedded-content-work-in-responsive-design/像这样:

<style>
.video-container { 
position: relative; 
padding-bottom: 56.25%; 
padding-top: 35px; 
height: 0; 
overflow: hidden; 
}
.video-container iframe { 
position: absolute; 
top:0; 
left: 0; 
width: 100%; 
height: 100%; 
}
</style>
<div class="video-container">
    <iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" allowfullscreen="" frameborder="0">
    </iframe>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 在 iframe 中嵌入视频未根据 webview 高度变化调整大小 的相关文章

随机推荐