简体   繁体   中英

Xamarin.Android - lock app in portrait orientation

I'm trying to lock my app's view to portrait. I've researched and came to this.

 [Activity(Label = "SuperBillViewActivity", Theme = "@style/MyTheme",
    ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]

However, my app does not get locked to portrait view. What can I do?

Make sure that you set ConfigurationChanges AND ScreenOrientation on every one of your activities:

ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation
ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait

ScreenOrientation :

Will set the activity's orientation.

ConfigurationChanges

Will force that the changes assigned to it will be handled by your Activity's code. In the case of ConfigChanges.Orientation , without this assignment, the Android OS will destroy and recreate Activity on Orientation changes. Normally this a good thing as your alternative landscape layout will be used, but in this case you want to prevent this re-creation process.

Example:

[Activity(Label = "Orientation", MainLauncher = true, Icon = "@mipmap/icon", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation, ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM