版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36347817/article/details/81566514
打开AndroidManifest.xml文件:
在activity属性上加上:android:screenOrientation="portrait" //竖屏显示
在activity属性上加上:android:screenOrientation="landscape" //横屏显示
代码实现:
/**
* 设置为横屏
*/
if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
关于Android中Activity的横竖屏切换问题可以通过AndroidManifest.xml文件中的Activity来配置:
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nonsensor"]
screenOrientation 用来指定Activity的在设备上显示的方向,每个值代表如下含义:
"unspecified " |
默认值 由系统来判断显示方向.判定的策略是和设备相关的,所以不同的设备会有不同的显示方向. |
"landscape " |
横屏显示(宽比高要长) |
"portrait " |
竖屏显示(高比宽要长) |
"user " |
用户当前首选的方向 |
"behind " |
和该Activity下面的那个Activity的方向一致(在Activity堆栈中的) |
"sensor " |
有物理的感应器来决定。如果用户旋转设备这屏幕会横竖屏切换。 |
"nosensor " |
忽略物理感应器,这样就不会随着用户旋转设备而更改了 ( "unspecified "设置除外 )。 |