How to Disable landscape mode in Android?

前端 未结 30 2664
Happy的楠姐
Happy的楠姐 2020-11-22 13:45

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

相关标签:
30条回答
  • 2020-11-22 14:13

    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 some devices where portrait is not the clearly popular orientation. When users are on devices with hardware keyboards or game pads a la the Nvidia Shield, on Chromebooks, on foldables, or on Samsung DeX, forcing portrait can make your app experience either limiting or a giant usability hassle. If your app doesn't have a strong UX argument that would lead to a negative experience for supporting other orientations, you should probably not force landscape. I'm talking about things like "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.
    0 讨论(0)
  • 2020-11-22 14:13
    <android . . . >
        . . .
        <manifest . . . >
            . . .
            <application>
                <activity android:name=".MyActivity" 
                    android:screenOrientation="portrait" 
                    android:configChanges="keyboardHidden|orientation">
                </activity>
            </application>
        </manifest>
    </android>
    
    0 讨论(0)
  • 2020-11-22 14:14

    You should change android:screenOrientation="sensorPortrait" in AndroidManifest.xml

    0 讨论(0)
  • 2020-11-22 14:14

    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);
    
    }
    
    0 讨论(0)
  • 2020-11-22 14:14

    Either in manifest class.

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

    or programmatically

    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    

    Note : you should call this before setContentView method for your activity in onCreate().

    0 讨论(0)
  • 2020-11-22 14:14

    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">
    
    0 讨论(0)
提交回复
热议问题