How do I disable orientation change on Android?

后端 未结 12 994
一向
一向 2020-11-22 02:45

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation=\"portrait\" in the manifest XML. This works OK for the HTC

相关标签:
12条回答
  • 2020-11-22 02:50

    In your androidmanifest.xml file:

       <activity android:name="MainActivity" android:configChanges="keyboardHidden|orientation">
    

    or

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    0 讨论(0)
  • 2020-11-22 02:50

    Please note, none of the methods seems to work now!

    In Android Studio 1 one simple way is to add android:screenOrientation="nosensor".

    This effectively locks the screen orientation.

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

    You need to modify AndroidManifest.xml as Intrications (previously Ashton) mentioned and make sure the activity handles the onConfigurationChanged event as you want it handled. This is how it should look:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    0 讨论(0)
  • 2020-11-22 02:56

    As said, set android:configChanges of your Activity (in manifest file) to keyboardHidden|orientation and then:

    1) Override onConfigurationChanged()

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //here you can handle orientation change
    }
    

    2) Add this line to your activity's onCreate()

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    It's better than add same line to onConfigurationChanged, because your app will turn to portrait mode and then back to landscape (it will happen only one time, but it's annoying).

    Also you can set android:screenOrientation="nosensor" for your activity (in manifest). But using this way you're a not able to handle orientation changes at all.

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

    In Visual Studio Xamarin:

    1. Add:

    using Android.Content.PM; to you activity namespace list.

    1. Add:

    [Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]

    as an attribute to you class, like that:

    [Activity(ScreenOrientation = ScreenOrientation.Portrait)]
    public class MainActivity : Activity
    {...}
    
    0 讨论(0)
  • 2020-11-22 02:57

    In OnCreate method of your activity use this code:

        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    

    Now your orientation will be set to portrait and will never change.

    0 讨论(0)
提交回复
热议问题