cocos creator webview andorid切换竖屏 闪退。请给出一个完美的解决方案
时间: 2023-07-29 19:07:15 浏览: 267
在Android平台上,当你使用WebView来加载网页时,切换屏幕的方向可能会导致应用程序崩溃。这是因为Android平台上的WebView存在一个缺陷,它不能正确地处理方向的变化。下面是一种解决此问题的方法:
1. 在Cocos Creator中,使用cc.sys.isMobile判断是否在移动设备上运行。
2. 如果是移动设备,则使用cc.view.enableAutoFullScreen(true)启用全屏模式。
3. 将WebView的Activity的屏幕方向设置为竖屏,并添加以下代码:
```java
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
```
4. 在Activity的onPause()和onResume()方法中添加以下代码:
```java
@Override
protected void onPause() {
super.onPause();
webView.onPause();
}
@Override
protected void onResume() {
super.onResume();
webView.onResume();
}
```
5. 在AndroidManifest.xml文件中添加以下代码:
```xml
<activity
android:name="com.example.MyWebViewActivity"
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait" />
```
这个方案需要你自己创建一个WebView的Activity,在Cocos Creator中调用该Activity来显示WebView。这样就能避免WebView在竖屏和横屏之间切换时出现崩溃的问题。
阅读全文
相关推荐












