前言
本文主要是将最近工作中遇到的一个问题进行总结分享,主要介绍的是如何让WebView中H5页面全屏播放视频。关于这个问题,做一下简单分析,希望对大家有所帮助,下面话不多说了,来看看详细的介绍吧。
效果图

运行效果
其实很简单,就是配置问题。关键地方配好了,基本没什么问题了。
在清单需要配置的AndroidManifest.xml
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".WebViewActivity" android:configChanges="orientation|screenSize|keyboardHidden" android:hardwareAccelerated="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
这里需要注意的是:启动硬件加速可以 在application 启动这个硬件加速,也可以在对应的activity启动
android:hardwareAccelerated="true"
还有这个
configChanges="orientation|screenSize|keyboardHidden"
必须是
orientation|screenSize|keyboardHidden
当然记得加上网络权限
<uses-permission android:name="android.permission.INTERNET" />
下面给出全部源码
activity_webview.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/mWebView" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
WebViewActivity源码
public class WebViewActivity extends AppCompatActivity {
 private FrameLayout mFrameLayout;
 private WebView mWebView;
 private MyWebChromeClient mMyWebChromeClient;
 private String URL = "http://m.tv.sohu.com/20130704/n380744170.shtml";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_webview);
 mFrameLayout = (FrameLayout) findViewById(R.id.mFrameLayout);
 mWebView = (WebView) findViewById(R.id.mWebView);
 initWebView();
 mWebView.loadUrl(URL);
 }
 private void initWebView() {
 WebSettings settings = mWebView.getSettings();
 settings.setJavaScriptEnabled(true);
 settings.setJavaScriptCanOpenWindowsAutomatically(true);
 settings.setPluginState(WebSettings.PluginState.ON);
 settings.setAllowFileAccess(true);
 settings.setLoadWithOverviewMode(true);
 settings.setUseWideViewPort(true);
 settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
 settings.setCacheMode(WebSettings.LOAD_DEFAULT);
 mMyWebChromeClient = new MyWebChromeClient();
 mWebView.setWebChromeClient(mMyWebChromeClient);
 mWebView.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
  view.loadUrl(url);
  return true;
  }
  @Override
  public void onPageFinished(WebView view, String url) {
  super.onPageFinished(view, url);
  }
 });
 }
 private class MyWebChromeClient extends WebChromeClient {
 private View mCustomView;
 private CustomViewCallback mCustomViewCallback;
 @Override
 public void onShowCustomView(View view, CustomViewCallback callback) {
  super.onShowCustomView(view, callback);
  if (mCustomView != null) {
  callback.onCustomViewHidden();
  return;
  }
  mCustomView = view;
  mFrameLayout.addView(mCustomView);
  mCustomViewCallback = callback;
  mWebView.setVisibility(View.GONE);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 }
 public void onHideCustomView() {
  mWebView.setVisibility(View.VISIBLE);
  if (mCustomView == null) {
  return;
  }
  mCustomView.setVisibility(View.GONE);
  mFrameLayout.removeView(mCustomView);
  mCustomViewCallback.onCustomViewHidden();
  mCustomView = null;
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  super.onHideCustomView();
 }
 }
 @Override
 public void onConfigurationChanged(Configuration config) {
 super.onConfigurationChanged(config);
 switch (config.orientation) {
  case Configuration.ORIENTATION_LANDSCAPE:
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  break;
  case Configuration.ORIENTATION_PORTRAIT:
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  break;
 }
 }
 @Override
 public void onPause() {
 super.onPause();
 mWebView.onPause();
 }
 @Override
 public void onResume() {
 super.onResume();
 mWebView.onResume();
 }
 @Override
 public void onBackPressed() {
 if (mWebView.canGoBack()) {
  mWebView.goBack();
  return;
 }
 super.onBackPressed();
 }
 @Override
 public void onDestroy() {
 super.onDestroy();
 mWebView.destroy();
 }
}
以上就是这篇文章的全部内容了,希望本文的内容对各位Android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。