折叠屏手机内屏与外屏切换时会默认触发oncreate等一系列事件,并且因为屏幕尺寸的改变导致显示适配问题,需要在 AndroidManifest.xml 对应的activity上添加 android:configChanges=”screenLayout”
1
| android:configChanges="screenSize|smallestScreenSize|screenLayout"
|
添加后只会触发 onConfigurationChanged 事件,所以可以在 onConfigurationChanged 里适配屏幕的尺寸
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @Override public void onConfigurationChanged(Configuration newConfig){ setWebView(); super.onConfigurationChanged(newConfig); }
private void setWebView(){ Display display = GameCenter.this.getWindowManager().getDefaultDisplay(); Point outSize = new Point(); display.getSize(outSize); int x = outSize.x; int y = outSize.y; if(x>1700 && y>1700){ int _x = y * 9 / 16; LinearLayout.LayoutParams p= new LinearLayout.LayoutParams(_x,y); p.gravity = Gravity.CENTER; mWebView.setLayoutParams(p); } else { LinearLayout.LayoutParams p= new LinearLayout.LayoutParams(x,y); p.gravity = Gravity.CENTER; mWebView.setLayoutParams(p); } }
|