我是Xamarin的新手,以前曾与android一起工作过,现在我正在尝试制作板子类,但似乎无法动态创建按钮。
我正在尝试生成按钮,由于某种原因,我现在在尝试动态制作按钮时遇到了这个问题... 希望你能帮助我:)
处于活动中:
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]);
}
}
}
}
}
在 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>
答案 0 :(得分:2)
处于活动中:
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]);
}
}
}
}
}
在 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>
ps :我运行了它,可以运行,但是按钮重叠,因此在计算代码中Button的位置时可能会出现问题。