C# 在Xamarin中动态创建按钮
我是新来的Xamarin以前使用过android,现在我正在尝试创建一个board类,但似乎无法动态创建按钮 我正在尝试生成按钮,由于某些原因,现在我在尝试动态生成按钮时遇到了这个问题。。。 希望你能帮助我:) 在活动中:C# 在Xamarin中动态创建按钮,c#,android,xamarin,layout,dynamically-generated,C#,Android,Xamarin,Layout,Dynamically Generated,我是新来的Xamarin以前使用过android,现在我正在尝试创建一个board类,但似乎无法动态创建按钮 我正在尝试生成按钮,由于某些原因,现在我在尝试动态生成按钮时遇到了这个问题。。。 希望你能帮助我:) 在活动中: using Android.App; using Android.OS; using Android.Support.V7.App; using Android.Runtime; using Android.Widget; using Android.Content; us
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
使用Android.App;
使用Android.OS;
使用Android.Support.V7.App;
使用Android.Runtime;
使用Android.Widget;
使用Android.Content;
使用Android.Views;
名称空间App2
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
董事会=新董事会(本);
SetContentView(Resource.Layout.activity_main);
}
}
班级委员会
{
相对布局;
语境;
常数int长度=8;
按钮[,]按钮;
公共董事会(背景)
{
布局=(作为MainActivity的上下文).FindViewById(Resource.Id.root\u布局);
this.context=上下文;
Init();
}
私有void Init()
{
//初始化电路板
按钮=新按钮[长度,长度];
for(int i=0;i
在axml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
在活动中:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
使用Android.App;
使用Android.OS;
使用Android.Support.V7.App;
使用Android.Runtime;
使用Android.Widget;
使用Android.Content;
使用Android.Views;
名称空间App2
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
董事会=新董事会(本);
SetContentView(Resource.Layout.activity_main);
}
}
班级委员会
{
相对布局;
语境;
常数int长度=8;
按钮[,]按钮;
公共董事会(背景)
{
布局=(作为MainActivity的上下文).FindViewById(Resource.Id.root\u布局);
this.context=上下文;
Init();
}
私有void Init()
{
//初始化电路板
按钮=新按钮[长度,长度];
for(int i=0;i
在axml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
ps:我运行它,它可以工作,但是按钮重叠,在计算代码中按钮的位置时可能会出现问题。
在活动中:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Content;
using Android.Views;
namespace App2
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
Board board = new Board(this);
SetContentView(Resource.Layout.activity_main);
}
}
class Board
{
RelativeLayout layout;
Context context;
const int length = 8;
Button[,] buttons;
public Board(Context context)
{
layout = (context as MainActivity).FindViewById<RelativeLayout>(Resource.Id.root_layout);
this.context = context;
Init();
}
private void Init()
{
//Initializing the board
buttons = new Button[length, length];
for (int i = 0; i < length; i++)
{
for (int j = 0; j < length; j++)
{
buttons[i, j] = new Button(context);
buttons[i, j].Text = (i + j).ToString();
buttons[i, j].SetX(50 * i);
buttons[i, j].SetY(20 * j);
layout.AddView(buttons[i, j]);
}
}
}
}
}
使用Android.App;
使用Android.OS;
使用Android.Support.V7.App;
使用Android.Runtime;
使用Android.Widget;
使用Android.Content;
使用Android.Views;
名称空间App2
{
[活动(Label=“@string/app_name”,Theme=“@style/AppTheme”,MainLauncher=true)]
公共类MainActivity:AppCompativeActivity
{
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
董事会=新董事会(本);
SetContentView(Resource.Layout.activity_main);
}
}
班级委员会
{
相对布局;
语境;
常数int长度=8;
按钮[,]按钮;
公共董事会(背景)
{
布局=(作为MainActivity的上下文).FindViewById(Resource.Id.root\u布局);
this.context=上下文;
Init();
}
私有void Init()
{
//初始化电路板
按钮=新按钮[长度,长度];
for(int i=0;i
在axml中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@android:style/Theme.NoTitleBar">
</RelativeLayout>
ps:我运行它,它可以工作,但是按钮重叠,在计算代码中按钮的位置时可能会出现问题。非常感谢您,先生!祝你有愉快的一天!我刚遇到另一个问题。。当我运行它时,它告诉我何时:
layout.AddView(按钮[I,j])代码>该按钮不是一个实例,但我在几行中将其声明为一个新按钮back@EyalAbramovitch有什么问题,有什么错误消息吗?我不太明白你在描述什么,还有layout.AddView(按钮[I,j])代码>拼写错误请您更新上面的代码,让我看看