How to Disable landscape mode in Android?

前端 未结 30 2662
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 13:59

    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
    
    0 讨论(0)
  • 2020-11-22 13:59

    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.

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

    use

    android:configChanges="keyboardHidden|orientation"
    android:screenOrientation="portrait" 
    
    0 讨论(0)
  • 2020-11-22 14:02
     <?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.

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

    Add android:screenOrientation="portrait" in AndroidManifest.xml file.

    For example :

    <activity android:name=".MapScreen" 
     android:screenOrientation="portrait"></activity>
    
    0 讨论(0)
  • Just add this attribute in your activity tag.

     android:screenOrientation="portrait"
    
    0 讨论(0)
提交回复
热议问题