我想在webView中为YouTube视频添加全屏模式。我发现了示例https://stackoverflow.com/a/16179544/5070495。我已经按照所有步骤进行操作,修改了代码,但是我遇到了问题。

我在rootView中使用layout,但是在getLayoutInflater()之前我不知道如何使用它。

问题是 :

View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments


所有代码:

public class vod extends Fragment {
    private WebView wv7;
    public static final String TAG = "VOD";
    private VideoEnabledWebView webView;
    private VideoEnabledWebChromeClient webChromeClient;


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


        View rootView = inflater.inflate(R.layout.vod, container, false);
        // Set layout
        //rootView.findViewById(R.layout.vld);

        // Save the web view
        wv7 = (VideoEnabledWebView) rootView.findViewById(R.id.webView7);

        // Initialize the VideoEnabledWebChromeClient and set event handlers
        View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout); // Your own view, read class comments
        ViewGroup videoLayout = (ViewGroup) rootView.findViewById(R.id.videoLayout); // Your own view, read class comments
        View loadingView = getLayoutInflater().inflate(R.layout.view_loading_video, null); // Your own view, read class comments
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
        {
            // Subscribe to standard events, such as onProgressChanged()...
            @Override
            public void onProgressChanged(WebView view, int progress) {
                // Your code...
            }
        };
        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) {
                        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) {
                        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });
        wv7.setWebChromeClient(webChromeClient);

        // Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
        wv7.loadUrl("http://m.youtube.com");


    }
    public void onBackPressed()
    {
        // Notify the VideoEnabledWebChromeClient, and handle it ourselves if it doesn't handle it
        if (!webChromeClient.onBackPressed())
        {
            if (wv7.canGoBack()) {
                wv7.goBack();
            }
        }
    }
}


谢谢您的帮助:p

最佳答案

为什么要调用getLayoutInflater?您有来自参数的充气机
更改:

View loadingView = inflater.inflate(R.layout.view_loading_video, null); // Your own view, read class comments


编辑:
第二种解决方案:

View loadingView = getActivity().getLayoutInflater().inflate(R.layout.view_loading_video, null);


但我认为第一个解决方案更好。

10-08 17:23