为什么全屏按钮没有出现在网页浏览活动中

时间:2016-05-25 19:52:48

标签: java android webview android-mediaplayer

我已经在我的片段中启用了VideoEnabledWebView,在观看视频时webview上没有出现全屏按钮,原因是什么?

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

    String url = "http://youtube.com";

    myWebView = (VideoEnabledWebView) rootView.findViewById(R.id.webViewTop);
    myWebView.setWebChromeClient(new WebChromeClient());
    myWebView.getSettings().setJavaScriptEnabled(true);
    progress = (ProgressBar) rootView.findViewById(R.id.progressBar3);
    View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout);
    ViewGroup videoLayout = (ViewGroup)rootView.findViewById(R.id.videoLayout);
    setHasOptionsMenu(true);
    webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, myWebView);

    myWebView.loadUrl(url);

webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
             {
                @Override
                public void toggledFullscreen(boolean fullscreen)
                {

                    if (fullscreen)
                    {
                        WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                        attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                        getActivity().getWindow().setAttributes(attrs);
                        if (android.os.Build.VERSION.SDK_INT >= 14)
                        {
                            //noinspection all
                            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                        }
                    }
                    else
                    {
                        WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                        attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                        attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                        getActivity().getWindow().setAttributes(attrs);
                        if (android.os.Build.VERSION.SDK_INT >= 14)
                        {
                            //noinspection all
                            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                        }
                    }

                }

            });

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView View, String url) {
            View.loadUrl(url);
            TopRatedFragment.this.progress.setProgress(0);

            return true;
        }
    });
    myWebView.setOnKeyListener(new android.view.View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                WebView webView = (WebView) v;

                switch (keyCode) {
                    case KeyEvent.KEYCODE_BACK:
                        if (webView.canGoBack()) {
                            webView.goBack();
                            return true;
                        }
                        break;
                }
            }

            return false;
        }
    });
   return rootView;

    }
    }

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<RelativeLayout
    android:id="@+id/nonVideoLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ProgressBar
        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="3dip"
        android:max="100"
        android:progressDrawable="@drawable/greenprogress"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />
    <com.taadu.slidechat.adaptor.VideoEnabledWebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webViewTop"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/progressBar3"/>

   </RelativeLayout>
    <RelativeLayout
        android:id="@+id/videoLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </RelativeLayout>
   </RelativeLayout>

搜索了几个小时,仍无法找到与启用全屏按钮相关的任何内容,

请帮忙。

更新

添加此库https://github.com/delight-im/Android-AdvancedWebView会在视频上显示全屏按钮,但在点击完整屏幕按钮后,它会全部挂起。

1 个答案:

答案 0 :(得分:0)

修正了

我没有调用videoenabledchromeclient,这就是为什么它没有进入全屏模式。这是固定代码;

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);

    String url = "http://youtube.com";
    //layout
    myWebView = new VideoEnabledWebView(getActivity());
    myWebView = (VideoEnabledWebView) rootView.findViewById(R.id.webViewTop);
    mSwipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swiperefresh);
    progress = (ProgressBar) rootView.findViewById(R.id.progressBar3);
    View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout);
    ViewGroup videoLayout = (ViewGroup)rootView.findViewById(R.id.videoLayout);
    View loadingView = getActivity().getLayoutInflater().inflate(R.layout.view_loading_video, null);
webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout,  videoLayout, loadingView, myWebView)
    {
 public void onProgressChanged(WebView view, int newProgress) {

         progress.setProgress(newProgress);
         progress.setVisibility(View.VISIBLE);
          setRefreshActionButtonState(true);


          if (newProgress == 100) {
           progress.setVisibility(View.GONE);
           setRefreshActionButtonState(false);
              mSwipeRefreshLayout.setRefreshing(false);


             }
           }
         };

webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
    {
        @Override
        public void toggledFullscreen(boolean fullscreen)
        {
            // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
            if (fullscreen)
            {

                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getActivity().getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                }
            }
            else
            {
                WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                getActivity().getWindow().setAttributes(attrs);
                if (android.os.Build.VERSION.SDK_INT >= 14)
                {
                    //noinspection all
                    getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
            }

        }
    });
    myWebView.setWebChromeClient(webChromeClient);

  return rootview;}