[英]Cannot Lock the Orientation to Portrait
It is said here and here that using ScreenOrientation = ScreenOrientation.Portrait
can force-lock the app's orientation: 据说这里和这里使用
ScreenOrientation = ScreenOrientation.Portrait
可以强制锁定应用的方向:
[Activity(
Label = "SomeApplication",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
ScreenOrientation = ScreenOrientation.Portrait
)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
Also tried editing AndroidManifest.xml
like this: 还尝试像这样编辑
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.SomeApplication">
<uses-sdk android:minSdkVersion="15" />
<application android:label="SomeApplication.Android">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
and this: 和这个:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.package.SomeApplication">
<uses-sdk android:minSdkVersion="15" />
<application android:label="SomeApplication.Android">
</application>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
</manifest>
But every time I run the app, I can still rotate it in landscape mode. 但是每次运行该应用程序时,我仍然可以在横向模式下旋转它。 My phone also has
Auto-Rotate
enabled, but I don't think that is an issue. 我的手机还启用了
Auto-Rotate
功能,但我认为这不是问题。
What else did I miss? 我还想念什么?
remove this code from MainActivity
and add this only in AndroidManifest.xml
从
MainActivity
删除此代码,并将其仅添加到AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
</application>
Another way is to set the orientation programmatically: Just call 另一种方法是通过编程设置方向:只需调用
this.RequestedOrientation = ScreenOrientation.Portrait; in OnResume()
to set it to portrait mode 设置为纵向模式
Because xamarin suggested the way you implemented first and failed. 因为xamarin提出了您首先实现但失败的方法。 https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/
https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.