How to Disable landscape mode in Android?

前端 未结 30 2666
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: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>
    
    0 讨论(0)
  • 2020-11-22 14:25

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

    Add below commend on your project,

    npm install

    npm i react-native-orientation-locker

    then you use manifest class like, React_Native (Your Project Folder)/ android/app/src/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    
    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
    

    Thank You!

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

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

    Just add Like this Line in Your Manifest

    android:screenOrientation="portrait"

    <manifest
        package="com.example.speedtest"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    
    
            <activity
                android:name="ComparisionActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait" >
            </activity>
    
        </application>
    
    </manifest>   
    
    0 讨论(0)
  • 2020-11-22 14:26

    Use this in onCreate() of the Activity

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
    0 讨论(0)
提交回复
热议问题