【问题标题】:Screen turns white after expanding to fullscreen展开全屏后屏幕变白
【发布时间】:2017-08-25 15:46:37
【问题描述】:

在 USB 上调试时,我只想将 WebView 扩展到全屏。不幸的是,出现了白屏,之后什么也没有。我真的不知道该怎么办。我制作了 CustomView,一切看起来都很好。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       filmkey = "<iframe width=\"96%\" height=\"96%\" src=\"https://www.youtube.com/embed/EtMOgsWEAmQ\" frameborder=\"0\" allowfullscreen></iframe>";

        screen=(WebView)findViewById(R.id.webView);
        screen.getSettings().setJavaScriptEnabled(true);
        String myvideokey = filmkey;
        screen.loadData(myvideokey, "text/html", "utf-8");
        screen.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onShowCustomView(View view, CustomViewCallback callback) {
                super.onShowCustomView(view, callback);
            }

            @Override
            public void onHideCustomView() {
                super.onHideCustomView();
            }
        });

}

【问题讨论】:

    标签: java android video webview fullscreen


    【解决方案1】:

    试试下面的代码。在这个全屏模式中,我创建了一个View,它将变成全屏模式。

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (savedInstanceState != null) {
                screen.restoreState(savedInstanceState);
            }
    
            setContentView(R.layout.activity_support);
            filmkey = "<iframe width=\"96%\" height=\"96%\" src=\"https://www.youtube.com/embed/EtMOgsWEAmQ\" frameborder=\"0\" allowfullscreen></iframe>";
    
            screen=(WebView)findViewById(R.id.webview);
            screen.getSettings().setJavaScriptEnabled(true);
            String myvideokey = filmkey;
            screen.loadData(myvideokey, "text/html", "utf-8");
            CustomWebChromeClient custom = new CustomWebChromeClient();
            screen.setWebChromeClient(custom);
        }
    

    而自定义的WebChromeClient

    class `CustomWebChromeClient` extends WebChromeClient{
    
    
    @Override
            public void onShowCustomView(View view, final CustomViewCallback callback) {
                super.onShowCustomView(view, callback);
                Dialog dialog = new Dialog(SupportActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                view.setBackgroundColor(Color.TRANSPARENT);
                dialog.setContentView(view);
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        callback.onCustomViewHidden();
                    }
                });
                dialog.show();
            }
    
        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
        }
    }
    

    并确保在活动中使用所有这些方法来处理webView 中的数据。

    @Override
        protected void onResume() {
            super.onResume();
            screen.onResume();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            screen.onPause();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            screen.stopLoading();
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            screen.destroy();
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            screen.saveState(outState);
        }
    

    希望对你有帮助。

    【讨论】:

    • "对话框对话框 = new Dialog(SupportActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);"这里出现一个错误,强调“SupportActivity.this”(在我的情况下为“WatchingActivity.this”)说“WatchingActivity 不是封闭类”
    • 只有在watchingActivity中创建类才不会出现这个错误。所有上述代码都在一个文件中,仅在您的情况下 WatchinActivity
    • 它仍然出现
    • 很抱歉它现在没有出现:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多