Lotus@
2009-02-24 15:29
采纳率: 50%
浏览 567

在 Android 中禁用景观模式?

How can I disable landscape mode for some of the views in my Android app?

转载于:https://stackoverflow.com/questions/582185/disable-landscape-mode-in-android

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

30条回答 默认 最新

  • 游.程 2009-02-24 17:04
    已采纳

    Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. For example:

    <activity android:name=".SomeActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" />
    

    EDIT: Since this has become a super-popular answer, I feel very guilty as forcing portrait is rarely the right solution to the problems it's frequently applied to.
    The major caveats with forced portrait:

    • This does not absolve you of having to think about activity lifecycle events or properly saving/restoring state. There are plenty of things besides app rotation that can trigger an activity destruction/recreation, including unavoidable things like multitasking. There are no shortcuts; learn to use bundles and retainInstance fragments.
    • Keep in mind that unlike the fairly uniform iPhone experience, there are many devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Droid 1-4, Xperia Play, or the Nvidia Shield, forcing portrait can make your app a giant usability hassle (especially on the Shield). If your app doesn't have an extremely specific use case that would lead to an outright negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like, "this is a remote control app for a phone with an IR blaster on only one side of the device," or "this is a cash register app for one specific model of tablet always used in a fixed hardware dock."

    So most apps should just let the phone sensors, software, and physical configuration make their own decision about how the user wants to interact with your app. A few cases you may still want to think about, though, if you're not happy with the default behavior of sensor orientation in your use case:

    • If your main concern is accidental orientation changes mid-activity that you think the device's sensors and software won't cope with well (for example, in a tilt-based game) consider supporting landscape and portrait, but using nosensor for the orientation. This forces landscape on most tablets and portrait on most phones, but I still wouldn't recommend this for most "normal" apps (some users just like to type in the landscape softkeyboard on their phones, and many tablet users read in portrait - and you should let them).
    • If you still need to force portrait for some reason, sensorPortrait may be better than portrait for Android 2.3+; this allows for upside-down portrait, which is quite common in tablet usage.
    已采纳该答案
    打赏 评论
  • 撒拉嘿哟木头 2010-10-18 14:53

    I was not aware of the AndroidManifest.xml file switch until reading this post, so in my apps I have used this instead:

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);     //  Fixed Portrait orientation
    
    打赏 评论
  • 10.24 2011-09-19 10:46

    In the <apphome>/platform/android directory created AndroidManifest.xml (copying it from the generated one). Then add android:screenOrientation="portrait" to ALL of the activity elements.

    打赏 评论
  • 三生石@ 2012-02-01 07:42

    add android:screenOrientation="portrait" in AndroidManifest.xml file .example <activity android:name=".MapScreen" android:screenOrientation="portrait"></activity>

    打赏 评论
  • perhaps? 2012-02-10 04:46

    If you want user-settings,

    then I'd recommend setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    You can change the settings from a settings menu.

    I require this because my timers must correspond to what's on the screen, and rotating the screen will destroy the current activity.

    打赏 评论
  • 乱世@小熊 2012-05-14 16:42

    use

    android:configChanges="keyboardHidden|orientation"
    android:screenOrientation="portrait" 
    
    打赏 评论
  • 狐狸.fox 2012-09-04 06:30
    <android . . . >
        . . .
        <manifest . . . >
            . . .
            <application>
                <activity android:name=".MyActivity" 
                    android:screenOrientation="portrait" 
                    android:configChanges="keyboardHidden|orientation">
                </activity>
            </application>
        </manifest>
    </android>
    
    打赏 评论
  • 谁还没个明天 2012-09-27 09:47

    Add this android:screenOrientation="portrait" in your manifest file where you declare your activity like this

    <activity android:name=".yourActivity"
        ....
        android:screenOrientation="portrait" />
    

    If you want to do using java code try

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    

    before you call setContentView method for your activity in onCreate().

    Hope this help and easily understandable for all...

    打赏 评论
  • 谁还没个明天 2013-07-17 22:53

    In hopes to help someone else, the following attribute on the ACTIVITY in AndroidManifest.xml is all you need:

    android:configChanges="orientation"

    So, full activity node:

    <activity android:name="Activity1" 
        android:icon="@drawable/icon" 
        android:label="App Name" 
        android:configChanges="orientation">
    
    打赏 评论
  • perhaps? 2013-08-16 10:11

    Add following snippet in your Manifest file.

    <activity android:name=".yourActivity"
              ....
              android:screenOrientation="portrait"/>
    

    This support only portrait mode

    打赏 评论
  • larry*wei 2013-08-21 14:41

    A lot of the answers here are suggesting to use "portrait" in your AndroidManifest.xml file. This might seem like a good solution - but as noted in the documentation, you are singling out devices that may only have landscape. You are also forcing certain devices (that work best in landscape) to go into portrait, not getting the proper orientation.

    My suggestion is to use "nosensor" instead. This will leave the device to use its default preferred orientation, will not block any purchases/downloads on Google Play, and will ensure the sensor doesn't mess up your (NDK, in my case) game.

    打赏 评论
  • YaoRaoLov 2013-10-01 07:00

    Just add this attribute in your activity tag.

     android:screenOrientation="portrait"
    
    打赏 评论
  • 狐狸.fox 2014-01-03 06:04

    you should change android:screenOrientation="sensorPortait" in AndroidManifest.xml

    打赏 评论
  • 北城已荒凉 2014-01-26 09:30

    Add android:screenOrientation="portrait" to the activity you want to disable landscape mode.

    打赏 评论
  • 胖鸭 2015-02-04 07:23

    You must set the orientation of each activity.

    <activity
                    android:name="com.example.SplashScreen2"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.Black.NoTitleBar" >
                </activity>
                <activity
                    android:name="com.example.Registration"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.Black.NoTitleBar" >
                </activity>
                <activity
                    android:name="com.example.Verification"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.Black.NoTitleBar" >
                </activity>
                <activity
                    android:name="com.example.WelcomeAlmostDone"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.Black.NoTitleBar" >
                </activity>
                <activity
                    android:name="com.example.PasswordRegistration"
                    android:label="@string/app_name"
                    android:screenOrientation="portrait"
                    android:theme="@android:style/Theme.Black.NoTitleBar" >
                </activity>
    
    打赏 评论
  • local-host 2015-02-05 09:30

    Just add Like this Line in Your Manifest

    android:screenOrientation="portrait"

    <manifest
        package="com.example.speedtestmeter"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
            <activity
                android:name="MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:name="ComparisionActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
            </activity>
    
            <activity
                android:name="GaugeView"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
            </activity>
    
            <activity
                android:name="Resultactivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
            </activity>
    
        </application>
    
    </manifest>   
    
    打赏 评论
  • MAO-EYE 2015-03-16 05:25

    If you want to disable Landscape mode for your android app ( or a single activity) all you need to do is add,

    android:screenOrientation="portrait" to the activity tag in AndroidManifest.xml file.

    Like:

    <activity android:name="YourActivityName" 
        android:icon="@drawable/ic_launcher" 
        android:label="Your App Name" 
        android:screenOrientation="portrait">
    

    Another Way , Programmatic Approach.

    If you want to do this programatically ie. using Java code. You can do so by adding the below code in the Java class of the activity that you don't want to be displayed in landscape mode.

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    I hope it helps you .For more details you can visit here enter link description here

    打赏 评论
  • 三生石@ 2015-05-06 09:59

    Use this in onCreate() of the Activity

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    打赏 评论
  • derek5. 2016-01-02 04:54

    add class inside oncreate() method

     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    打赏 评论
  • 旧行李 2016-03-20 14:52

    If you are using Xamarin C# some of these solutions will not work. Here is the solution I found to work.

    [Activity(MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Portrait)]
    

    Above the class works well, similar to the other solutions, also not globally applicable and needs to be placed in each activity header.

    打赏 评论
  • 笑故挽风 2016-04-28 06:24

    How to change orientation in some of the view

    Instead of locking orientation of entire activity you can use this class to dynamically lock orientation from any of your view pragmatically:-

    Make your view Landscape

    OrientationUtils.lockOrientationLandscape(mActivity);
    

    Make your view Portrait

    OrientationUtils.lockOrientationPortrait(mActivity);
    

    Unlock Orientation

    OrientationUtils.unlockOrientation(mActivity);
    

    Orientation Util Class

    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);
        }
    
    }
    
    打赏 评论
  • 谁还没个明天 2016-09-22 15:14

    if your activity related to the first device orientation state,get the current device orientation in the onCreate method and then fix it forever:

            int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
    
            if(deviceRotation == Surface.ROTATION_0) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
            else if(deviceRotation == Surface.ROTATION_180)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            }
            else if(deviceRotation == Surface.ROTATION_90)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
            else if(deviceRotation == Surface.ROTATION_270)
            {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            }
    
    打赏 评论
  • 程序go 2017-01-16 06:25

    You can force your particular activity to always remain in portrait mode by writing this in your manifest.xml

     <activity android:name=".MainActivity"
            android:screenOrientation="portrait"></activity>
    

    You can also force your activity to remain in postrait mode by writing following line in your activity's onCreate() method

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_layout);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    }
    
    打赏 评论
  • 斗士狗 2017-02-28 09:26

    Simply add android:screenOrientation="portrait" to the activity tag in application manifest.

    Eg:

        <activity android:name=".ui.activities.MainActivity"
           android:screenOrientation="portrait"></activity>
    
    打赏 评论
  • 零零乙 2017-04-22 17:51

    If you don't want to go through the hassle of adding orientation in each manifest entry of activity better, create a BaseActivity class (inherits 'Activity' or 'AppCompatActivity') which will be inherited by every activity of your application instead of 'Activity' or 'AppCompatActivity' and just add the following piece of code in your BaseActivity:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // rest of your code......
    }
    
    打赏 评论
  • derek5. 2017-05-12 06:49

    You should put this code in manifests file.......

        <activity android:name=".SunderFlowersActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            >
        </activity>
    
    打赏 评论
  • 撒拉嘿哟木头 2017-06-11 15:07

    In kotlin same can be programatically achieved using below

    requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    
    打赏 评论
  • Memor.の 2017-07-26 12:03
     <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="in.co.nurture.bajajfinserv">
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    
    
        <application
    
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity" android:screenOrientation="portrait">
    
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    we can restrict the Activity in portrait or landscape mode by using the attribute or android:screenOrientation.

    if we have more than one activity in my program then you have freedom to restrict any one of activity in any one the mode and it never effect the others which you don't want.

    打赏 评论
  • 游.程 2018-02-06 09:53

    It worked for me, Try to add this code in AndroidManifest file

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            ....
            ....
            </application>
    
    打赏 评论
  • 乱世@小熊 2018-04-05 09:28

    You can do this for your entire application without having to make all your activities extend a common base class.

    The trick is first to make sure you include an Application subclass in your project. In its onCreate(), called when your app first starts up, you register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events.

    This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation() on the newly created activity.

    And do not forget to add app:name=".MyApp" in your manifest file.

    class MyApp extends Application {
    
    @Override
    public void onCreate() {
        super.onCreate();  
    
        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
    
            @Override
            public void onActivityCreated(Activity activity, 
                                          Bundle savedInstanceState) {
    
                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
            }
    
            ....
    
        });
    
    }
    

    }

    打赏 评论

相关推荐 更多相似问题