Xamarin Android 锁定方向仅在手机上显示为纵向

Phi*_*ray 8 android android-layout xamarin.android xamarin

我试图将手机大小布局上的整个应用程序的方向锁定为仅纵向,但在平板电脑大小的布局上允许纵向和横向。

我知道我可以将 Activity 归因于使用特定方向,但这适用于两种布局尺寸。

    [Activity(
        Label = "Brs.Members.Droid"
        , MainLauncher = true
        , Icon = "@mipmap/icon"
        , Theme = "@style/Theme.Splash"
        , NoHistory = true
        , ScreenOrientation = ScreenOrientation.Portrait)]
    public class SplashScreen : MvxSplashScreenActivity
    {
        public SplashScreen() : base(Resource.Layout.SplashScreen)
        {
        }
    }
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以只排除手机尺寸设备上的横向布局?

Mik*_* Ma 7

试试下面的代码,先检查你的设备是否是平板电脑,然后设置方向:

[Activity(Label = "MyOrientationDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        if (!isPad(this))
        {
            RequestedOrientation = ScreenOrientation.Portrait;
        }
        SetContentView (Resource.Layout.Main);
    }

    public static bool isPad(Context context)
    {
        return (context.Resources.Configuration.ScreenLayout& ScreenLayout.SizeMask)>= ScreenLayout.SizeLarge;
    }
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*dze 1

您可以做的是首先检查设备是否是平板电脑,然后在运行时将方向锁定为纵向。

这是一个有用的帖子,用于确定设备是否是平板电脑:
确定设备是智能手机还是平板电脑?

所以你可以这样做:

  1. 创建一个值资源文件,其限定符为最小屏幕宽度等于 600dp。(sw600dp)

    在文件内将 isTablet 的布尔值设置为 true :

    <resources>
        <bool name="isTablet">true</bool>
    </resources>
    
    Run Code Online (Sandbox Code Playgroud)

    然后在正常值文件(没有屏幕尺寸限定符)中将 isTablet 的值设置为 false :

    <resources>
        <bool name="isTablet">false</bool>
    </resources>
    
    Run Code Online (Sandbox Code Playgroud)
  2. 然后在 onCreate 中获取 isTablet 值并相应地设置方向:

    boolean isTablet = getResources().getBoolean(R.bool.isTablet);
    if (!isTablet) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
    Run Code Online (Sandbox Code Playgroud)