【问题标题】:Xamarin Android lock orientation to portrait on phone onlyXamarin Android 仅在手机上将方向锁定为纵向
【发布时间】:2017-02-21 14:53:32
【问题描述】:

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

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

有没有办法只在手机大小的设备上排除横向布局?

【问题讨论】:

    标签: android android-layout xamarin xamarin.android


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      这是一个有用的帖子,可以确定设备是否为平板电脑:
      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);
        }
        

      【讨论】:

      • 这似乎是更可取的解决方案,但我建议从链接的问题中添加更多细节。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多