如何在 WebView 中全屏显示 youtube 视频 [英] How to fullscreen youtube video in WebView

查看:26
本文介绍了如何在 WebView 中全屏显示 youtube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_web_view_play_youtube);
    WebView w = (WebView) findViewById(R.id.w);
    w.setWebChromeClient(new WebChromeClient());
    w.setWebViewClient(new WebViewClient());
    w.getSettings().setJavaScriptEnabled(true);
    w.loadUrl("https://www.youtube.com/watch?v=gY-HZg1Uwpc");
}

我得到以下截图

在此屏幕截图中,我看不到全屏"按钮.

In this screenshot, I could not see "fullScreen" button.

推荐答案

在您的情况下,您必须首先在 XML 文件中添加 FrameLayout.之后

In your case, you have to first add FrameLayout in your XML file. After that

你必须实现WebChromeClient的两个方法onShowCustomViewonHideCustomView,如下所示:

you have to implement two methods onShowCustomView and onHideCustomView of WebChromeClient as shown below:

FrameLayout customViewContainer = findViewById(R.id.customViewContainer);

webView.setWebChromeClient(new WebChromeClient() {

        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            super.onShowCustomView(view,callback);
            webView.setVisibility(View.GONE);
            customViewContainer.setVisibility(View.VISIBLE);
            customViewContainer.addView(view);
        }
        public void onHideCustomView () {
            super.onHideCustomView();
            webView.setVisibility(View.VISIBLE);
            customViewContainer.setVisibility(View.GONE);
        }
    });

这篇关于如何在 WebView 中全屏显示 youtube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆