【问题标题】:Cannot Lock the Orientation to Portrait无法将方向锁定为纵向
【发布时间】:2017-12-29 06:20:19
【问题描述】:

据说herehere使用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,但我认为这不是问题。

我还错过了什么?

【问题讨论】:

    标签: android xamarin xamarin.android


    【解决方案1】:

    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>
    

    【讨论】:

    • 我删除了MainActivity.cs 中的方向,并复制了您的&lt;application&gt;,但在调试时仍会旋转
    【解决方案2】:

    另一种方法是以编程方式设置方向: 打个电话就行了

    this.RequestedOrientation = ScreenOrientation.Portrait; in OnResume()

    将其设置为纵向模式

    因为 xamarin 建议了您首先实施的方式并失败了。 https://developer.xamarin.com/guides/xamarin-forms/user-interface/layouts/device-orientation/

    【讨论】:

    • App does not contain a definition of RequestedOrientation 放入OnResume()后出现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多