我如何禁用景观模式的一些观点在我的Android应用程序?


当前回答

把它写进你的载货单里。

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="sensorPortrait" />

方向将是纵向的,但如果用户的手机是上下颠倒的,它也会显示正确的方式。(这样你的屏幕就会旋转180度。)


如果活动在多窗口模式下运行,系统将忽略此属性。

更多:https://developer.android.com/guide/topics/manifest/activity-element

其他回答

你应该在AndroidManifest.xml中修改android:screenOrientation="sensorPortrait

你所需要的就是AndroidManifest.xml中活动的以下属性:

android:configChanges="orientation"

那么,完整的活动节点:

<activity
    android:name="Activity1"
    android:icon="@drawable/icon"
    android:label="App Name"
    android:configChanges="orientation">

在Activity的onCreate()中使用

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

如何在一些视图中改变方向

不是锁定整个活动的方向,你可以使用这个类从你的任何视图中动态地锁定方向:

让你的视图成为景观

OrientationUtils.lockOrientationLandscape(mActivity);

将视图设置为竖屏

OrientationUtils.lockOrientationPortrait(mActivity);

解锁取向

OrientationUtils.unlockOrientation(mActivity);

方向Util类

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Surface;
import android.view.WindowManager;

/*  * This class is used to lock orientation of android app in nay android devices
 */

public class OrientationUtils {
    private OrientationUtils() {
    }

    /** Locks the device window in landscape mode. */
    public static void lockOrientationLandscape(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }

    /** Locks the device window in portrait mode. */
    public static void lockOrientationPortrait(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    /** Locks the device window in actual screen mode. */
    public static void lockOrientation(Activity activity) {
        final int orientation = activity.getResources().getConfiguration().orientation;
        final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
                .getRotation();

        // Copied from Android docs, since we don't have these values in Froyo
        // 2.2
        int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
        int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9;

        // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO
        if (!(Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)) {
            SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }

        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) {
            if (orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
        }
    }

    /** Unlocks the device window in user defined screen mode. */
    public static void unlockOrientation(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }

}
<android . . . >
    . . .
    <manifest . . . >
        . . .
        <application>
            <activity
                android:name=".MyActivity"
                android:screenOrientation="portrait"
                android:configChanges="keyboardHidden|orientation">
            </activity>
        </application>
    </manifest>
</android>