Xamarin Android锁定方向仅适用于手机上的人像

时间:2017-02-21 14:53:32

标签: android android-layout xamarin xamarin.android

我正在尝试将手机大小的布局上的整个应用的方向锁定为仅限肖像,但允许在平板电脑大小的布局上使用纵向和横向。

我知道我可以将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)
        {
        }
    }

是否有方法只排除手机大小设备上的横向布局?

2 个答案:

答案 0 :(得分:4)

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

[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;
    }
}

答案 1 :(得分:0)

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

这是一个有用的帖子,用于确定设备是否为平板电脑:
Determine if the device is a smartphone or tablet?

以下是您可以做的事情:

  1. 使用最小屏幕宽度等于600dp的限定符创建值资源文件。 (sw600dp)

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

    <resources>
        <bool name="isTablet">true</bool>
    </resources>
    

    然后在普通值文件内(没有屏幕大小限定符)将isTablet的值设置为false:

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

    boolean isTablet = getResources().getBoolean(R.bool.isTablet);
    if (!isTablet) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    
相关问题