据说here和here使用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());
}
}
还尝试像这样编辑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>
和此:
<?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>
但每次我运行应用程序时,我仍然可以在横向模式下旋转它。我的手机也启用了Auto-Rotate
,但我认为这不是问题。
我还想念什么?
答案 0 :(得分:0)
从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>
答案 1 :(得分:0)
另一种方法是以编程方式设置方向: 只需致电
this.RequestedOrientation = ScreenOrientation.Portrait; in OnResume()
将其设置为纵向模式
因为xamarin建议你先实施并失败的方式。 https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/