本文介绍了Android嵌入iframe中的视频未在webview高度更改时调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在webview中播放嵌入的youtube视频。我的问题是当我旋转屏幕并调整webview高度时,iframe没有改变它的高度。

I am trying to play embeded youtube videos in webview. My problem is when I rotate screen and resize webview height, iframe is not changing its height.

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控件非常小。

But this code makes youtube Playback controls very small.

推荐答案

使用此,如下所示:

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

这篇关于Android嵌入iframe中的视频未在webview高度更改时调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 01:54
查看更多