Android 横竖屏总结
横竖屏切换后Activity会重新执行onCreat函数,但是在Android工程的Mainfest.xml中加入
android:screenOrientation="user" android:configChanges="orientation|keyboardHidden"之后,
横竖屏切换之后就不会去执行OnCreat函数了,而是会去调用onConfigurationChanged(),这样我们就能控制横竖屏的切换了。
或者在res目录下建立layout-land和layout-port目录,相应的layout文件不变。layout-land是横屏的layout,layout-port是竖屏的layout。
如果想让它一直是横屏显示的话,只要在配置文件中设置android:screenOrientation="landscape"就行了,
或者在代码中 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);。
如果想让它一直是竖屏显示的话,只要在配置文件中设置android:screenOrientation="portrait"就行了,
或者在代码中setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);。
启动的时候是横屏的话就横屏表示,纵屏的话就纵屏表示,手机切换横竖屏不能用。
第一步:在Mainfest.xml中追加android:screenOrientation="sensor" android:configChanges="orientation|keyboardHidden"
第二步:取得屏幕的长和宽,进行比较设置横竖屏的变量。
Display display = getWindowManager().getDefaultDisplay();int width = display.getWidth();int height = display.getHeight();if (width > height) {orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;} else {orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;}
第三步:在onConfigurationChanged()函数中追加this.setRequestedOrientation(mOrientation)就行了
@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);this.setRequestedOrientation(mOrientation);}
切到别的画面的时候再回到原画面,它就仍然是横的或者是纵的。想让它从别的屏幕回来后,又重新横竖屏布局,
只要在OnResume()中在设定下就行了,但是这个只支持横竖屏只有一个layout的。
更多相关文章
- C语言函数以及函数的使用
- android屏幕页面实现滚动,页面跳…
- android activity设置全屏以及Android获得屏幕的宽和高
- 【Android】如何调节屏幕亮度,关闭屏幕
- Android编程实现屏幕自适应方向尺寸与分辨率的方法
- 【Android NDK 开发】Ubuntu 函数库交叉编译 ( Android 动态库交