# Android-WebView中全屏问题

## 问题详述：

使用WebView加载SaaS-H5观看页时，点击全屏按钮无反应

## 解决方案：

1. WebSettings配置需要添加webSettings.setUseWideViewPort(true);
2. 处理自定义WebChromeClient

   ```java
   public class WVChromeClient extends WebChromeClient {
       private Context _context;
       private AndroidWebViewActivity _m;
       public WVChromeClient(Context context, AndroidWebViewActivity mainActivity)
       {
           _context = context;
           _m = mainActivity;
       }

       @Override
       public void onProgressChanged(WebView view, int newProgress) {
           super.onProgressChanged(view, newProgress);
           Log.d("MainActivity","newProgress："+ newProgress );
       }

       @Nullable
       @Override
       public Bitmap getDefaultVideoPoster() {
           return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
       }

       @Override
       public void onShowCustomView(View view, CustomViewCallback callback) {
           _m.showCustomView(view ,callback);
       }

       @Override
       public void onHideCustomView() {
           _m.hideCustomView();
       }
   }
   ```
3. 处理全屏，退出全屏

   ```java
   /** 视频全屏参数 */
       protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
       private View customView;
       private FrameLayout fullscreenContainer;
       private WebChromeClient.CustomViewCallback customViewCallback;
       /** 视频播放全屏 **/
       public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
           // if a view already exists then immediately terminate the new one
           if (customView != null) {
               callback.onCustomViewHidden();
               return;
           }
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
           AndroidWebViewActivity.this.getWindow().getDecorView();

           FrameLayout decor = (FrameLayout) getWindow().getDecorView();
           fullscreenContainer = new FullscreenHolder(AndroidWebViewActivity.this);
           fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
           decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
           customView = view;
           setStatusBarVisibility(false);
           customViewCallback = callback;
       }

       /** 隐藏视频全屏 */
       public void hideCustomView() {
           if (customView == null) {
               return;
           }
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
           setStatusBarVisibility(true);
           FrameLayout decor = (FrameLayout) getWindow().getDecorView();
           decor.removeView(fullscreenContainer);
           fullscreenContainer = null;
           customView = null;
           customViewCallback.onCustomViewHidden();
           webView.setVisibility(View.VISIBLE);
       }

       /** 全屏容器界面 */
       static class FullscreenHolder extends FrameLayout {

           public FullscreenHolder(Context ctx) {
               super(ctx);
               setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
           }

           @Override
           public boolean onTouchEvent(MotionEvent evt) {
               return true;
           }
       }

       private void setStatusBarVisibility(boolean visible) {
           int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
           getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       }

       @Override
       public boolean onKeyUp(int keyCode, KeyEvent event) {
           switch (keyCode) {
               case KeyEvent.KEYCODE_BACK:
                   /** 回退键 事件处理 优先级:视频播放全屏-网页回退-关闭页面 */
                   if (customView != null) {
                       hideCustomView();
                   } else if (webView.canGoBack()) {
                       webView.goBack();
                   } else {
                       finish();
                   }
                   return true;
               default:
                   return super.onKeyUp(keyCode, event);
           }
       }
   ```
4. Manifest中配置activity属性

   ```java
   <activity android:name=".AndroidWebViewActivity"
               android:configChanges="keyboardHidden|orientation|screenSize"/>
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://polyv.gitbook.io/document/docs/live/solution/common-problem/android/webview_full_screen.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
