LoginSignup
3
2

More than 5 years have passed since last update.

Xamarin.Androidで画面向き固定は、Activity属性への指定でいっぱつ!

Last updated at Posted at 2017-01-05

ネット上ではいろいろ紹介されていますが...

これらのどれも可能(かつ、それぞれ特性あり)なのだと思われますが、次の方法が一番簡単だと思います(当方比)

Xamarin 公式ガイドページより

コード例

Blank App のスケルトンをもとに...

using Android.App;
using Android.Widget;
using Android.OS;

namespace App2
{
    [Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon",
     ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape)] // <--横長画面に固定するときは Landscape。縦長固定なら Portrait。
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);
        }
    }
}

ActivityAttributeScreenOrientation に値を指定してやります。Activity 属性値指定箇所で、カンマを打って S を打ってみましょう。インテリセンスで自動補完されます。指定する値の意味は こちらをご参照あれ。ね、簡単でしょう?!
ちなみに、この ActivityAttribute では、ソフトウエアキーボードの挙動指定などもできるようです。

公式ガイド説明では、ActivityAttribute の振る舞いは「『AndroidManifest.xml』の要素を生成します」だそうです。
Java開発環境ではxmlに記載していた内容をC#のコードに集約させる方向なのかな、Xamarinの思想は。。確かに、単一のコードでプラットフォームをまたげることがXamarinの究極の目的ですからね。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2