前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。