Android开发之强制横屏和强制竖屏

强制竖屏设置

1.代码在Activity的onResume方法中添加如下代码

    @Override
    protected void onResume() {
        /**
         * 设置为横屏  
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();
    }

2.在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)

android:launchMode="singleTask" android:screenOrientation="portrait">

强制横屏设置

1.代码在Activity的onResume方法中添加如下代码

@Override
    protected void onResume() {
        /**
         * 设置为横屏  
         */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }

2.在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)

android:launchMode="singleTask" android:screenOrientation="landscape">

你可能感兴趣的:(小技巧)