質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

2664閲覧

Xamarin.AndroidでGridLayoutをC#コードのみで配置する方法

piston2438

総合スコア7

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2018/05/09 02:01

前提・実現したいこと

Xamarin.AndroidでGridLayoutをC#コードのみで配置する方法について御教示願います。
axmlを用いず、C#コードのみでGridLayout上にButtonを複数配置したいと思います。

該当のソースコード

Axmlでは以下の記述で実現できました。

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
app:columnCount="3"
app:rowCount="3"
app:useDefaultMargins="true">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" app:layout_row="0" app:layout_column="0" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" app:layout_rowSpan="2" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" app:layout_row="0" app:layout_column="1" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" app:layout_row="0" app:layout_column="2" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" app:layout_row="1" app:layout_column="1" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" app:layout_row="1" app:layout_column="2" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" app:layout_row="2" app:layout_column="0" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" app:layout_row="2" app:layout_column="1" app:layout_columnWeight="1" app:layout_gravity="fill" app:layout_rowWeight="1" app:layout_columnSpan="2" />
</android.support.v7.widget.GridLayout>

これをAxmlを用いず、C#コードのみで実現しようと以下のように書きました。

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

namespace App1
{
[Activity(Label = "App1", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource //SetContentView(Resource.Layout.Main); Android.Support.V7.Widget.GridLayout rootLayout = new Android.Support.V7.Widget.GridLayout(this); rootLayout.LayoutParameters = new Android.Support.V7.Widget.GridLayout.LayoutParams { Width = ViewGroup.LayoutParams.MatchParent, Height = ViewGroup.LayoutParams.MatchParent }; rootLayout.ColumnCount = 3; rootLayout.RowCount = 3; rootLayout.UseDefaultMargins = true; // SetContentView(rootLayout); // Button[] btn = new Button[7]; for(int i=0; i<7; i++) { btn[i] = new Button(this); btn[i].Text = i.ToString(); btn[i].LayoutParameters = new Android.Support.V7.Widget.GridLayout.LayoutParams { Width = ViewGroup.LayoutParams.WrapContent, Height = ViewGroup.LayoutParams.WrapContent }; // rowSpanを設定 if (0 == i) { } // columnSpanを設定 else if (6 == i) { } // rootLayout.AddView(btn[i]); } } }

}

不明な点

しかしながら
GridLayoutの「android:layout_margin」や
Buttonの、
位置指定「app:layout_row」「app:layout_column」
重み「app:layout_columnWeight」「app:layout_rowWeight」
Gravity「app:layout_gravity」
スパン「app:layout_rowSpan」「app:layout_CoiumnSpan」

以上をC#コードで設定する方法がわかりません。これらをどのように記述すればよろしいでしょうか?

御教示のほど、よろしくお願いいたします。

補足情報(FW/ツールのバージョンなど)

Visual studio 2017
Xamain 4.9.0.753
Xamain.Android SDK 8.2.0.16
Android SDK Tools 25.2.5
Android API 4.0.2

Androidサポートライブラリ
Xamarin.Android.Support.v7.GridLayout 27.0.2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

GridLayoutの「layout_margin」のみ未解決ですが、Buttonに関しては全て解決できました。
Android.Support.V7.Widget.GridLayoutのInvokeSpec()メソッドを使うことで、
位置指定、重み、スパンを指定できます。以下にソースコードを記載します。

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

namespace App1
{
[Activity(Label = "App1", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set our view from the "main" layout resource //SetContentView(Resource.Layout.Main); Android.Support.V7.Widget.GridLayout rootLayout = new Android.Support.V7.Widget.GridLayout(this); rootLayout.LayoutParameters = new Android.Support.V7.Widget.GridLayout.LayoutParams { Width = ViewGroup.LayoutParams.MatchParent, Height = ViewGroup.LayoutParams.MatchParent }; // rootLayout.ColumnCount = 3; rootLayout.RowCount = 3; rootLayout.UseDefaultMargins = true; // SetContentView(rootLayout); // Button[] btn = new Button[7]; for(int i=0; i<7; i++) { // int rowPos; int colPos; // int rowSpan = 1; int colSpan = 1; // switch (i) { case 0: rowPos = 0; colPos = 0; // rowSpan = 2; // break; case 1: rowPos = 0; colPos = 1; break; case 2: rowPos = 0; colPos = 2; break; case 3: rowPos = 1; colPos = 1; break; case 4: rowPos = 1; colPos = 2; break; case 5: rowPos = 2; colPos = 0; break; case 6: rowPos = 2; colPos = 1; // colSpan = 2; // break; default: rowPos = 0; colPos = 0; break; } // btn[i] = new Button(this); btn[i].Text = i.ToString(); btn[i].LayoutParameters = new Android.Support.V7.Widget.GridLayout.LayoutParams() { Width = ViewGroup.LayoutParams.WrapContent, Height = ViewGroup.LayoutParams.WrapContent, RowSpec = Android.Support.V7.Widget.GridLayout.InvokeSpec(rowPos, rowSpan, Android.Support.V7.Widget.GridLayout.FillAlignment, 1), // 1→Weight ColumnSpec = Android.Support.V7.Widget.GridLayout.InvokeSpec(colPos, colSpan, Android.Support.V7.Widget.GridLayout.FillAlignment, 1), // 1→Weight }; btn[i].Gravity = GravityFlags.Center; // rootLayout.AddView(btn[i]); } } }

}

投稿2018/05/09 13:13

編集2018/05/10 06:21
piston2438

総合スコア7

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問