默认情况下,当屏幕方面切换时,activity的onCreate()方法会被重新调用,所以可以在其中通过以下代码来读取屏的方向:
view plaincopy to clipboardprint?
public void onCreate() {
if(this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE) {Log.i("info", "landscape");
} else if (this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT) {Log.i("info", "portrait");
}
}
public void onCreate() {
if(this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE) {Log.i("info", "landscape");
} else if (this.getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT) {Log.i("info", "portrait");
}
}
如果在androidmanifest.xml中加入配置
android:configChanges="orientation|keyboardHidden|navigation
当屏幕翻转时,Activity就不会重复的调用onCreate()、onPause()和onResume().
而是调用onConfigurationChanged(Configuration newConfig)
如何取得Android屏幕大小:
view plaincopy to clipboardprint? int screenWidth,screenHeight; WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); screenWidth = display.getWidth(); screenHeight = display.getHeight(); int screenWidth,screenHeight; WindowManager windowManager = getWindowManager(); Display display = windowManager.getDefaultDisplay(); screenWidth = display.getWidth(); screenHeight = display.getHeight();
也有人提到另一种Android屏幕大小的更改方法:
view plaincopy to clipboardprint? DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels; int screenHeight = dm.heightPixels;
下一篇:Android中有什么屏幕元素