2017-08-25 59 views
0

在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(); 
      } 
     }); 

}

回答

0

嘗試下面的代碼。在這個全屏幕上,我創建了一個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); 
    } 

我希望它有幫助。

+0

「Dialog dialog = new Dialog(SupportActivity.this,android.R.style.Theme_Black_NoTitleBar_Fullscreen);」這裏出現一個錯誤,它強調了「SupportActivity.this」(在我的情況中爲「WatchingActivity.this」),並且說「WatchingActivity不是封閉類」 –

+0

使watchingActivity中的類只有這個errot不會來。所有上述代碼只在一個文件中在您的案例中只有一個WatchinActivity – Sahil

+0

它仍然出現 –