public class MyCustomWebViewClient extends WebViewClient { private View mCustomView; private WebChromeClient.CustomViewCallback mCustomViewCallback; private int mOriginalSystemUiVisibility;
@Override
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mCustomView = view;
mOriginalSystemUiVisibility = mActivity.getWindow().getDecorView().getSystemUiVisibility();
mCustomViewCallback = callback;
mActivity.setContentView(view);
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
@Override
public void onHideCustomView() {
mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mActivity.setContentView(mContentView);
mActivity.getWindow().getDecorView().setSystemUiVisibility(mOriginalSystemUiVisibility);
mCustomViewCallback.onCustomViewHidden();
mCustomView = null;
}
}
WebView webView = new WebView(mContext); webView.setWebViewClient(new MyCustomWebViewClient());
这样就可以在Android WebView中全屏播放HTML5视频,并且横屏显示。