C# xamarin创建一个每行有2个按钮的tablelayout
我一直在尝试创建一个两个按钮相邻的表格布局。我一直在阅读,但我似乎无法让它在屏幕上显示任何内容。这是我当前的TableLayout代码C# xamarin创建一个每行有2个按钮的tablelayout,c#,android,xamarin,tablelayout,C#,Android,Xamarin,Tablelayout,我一直在尝试创建一个两个按钮相邻的表格布局。我一直在阅读,但我似乎无法让它在屏幕上显示任何内容。这是我当前的TableLayout代码 TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout); TableLayout llInner = new TableLayout(this); TableLayout.LayoutParams lp = new TableLayout
TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout);
TableLayout llInner = new TableLayout(this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
llInner.Orientation = Orientation.Horizontal;
llInner.LayoutParameters = lp;
llInner.WeightSum = 2;
ll.AddView(llInner);
var i = 0;
var row = new TableRow(this);
//TableRow.LayoutParams rowlayout = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent);
row.LayoutParameters = lp;
foreach (var series1 in series)
{
var b = new Button(this);
b.Text = series1.Series;
//lp = new TableLayout.LayoutParams(1, LinearLayout.LayoutParams.WrapContent);
b.LayoutParameters = lp;
row.AddView(b);
i =+ 1;
if (i > 1)
{
llInner.AddView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent, TableLayout.LayoutParams.WrapContent));
row = new TableRow(this);
row.LayoutParameters = lp;
i = 0;
}
}
TableLayout ll=findviewbyd(Resource.Id.buttonLayout);
TableLayout Lliner=新的TableLayout(本);
TableLayout.LayoutParams lp=新的TableLayout.LayoutParams(LinearLayout.LayoutParams.FillParent,LinearLayout.LayoutParams.WrapContent);
Lliner.Orientation=Orientation.Horizontal;
LLINER.LayoutParameters=lp;
Lliner.WeightSum=2;
ll.AddView(LLINER);
var i=0;
var row=新表格行(此);
//TableRow.LayoutParams rowlayout=新建TableRow.LayoutParams(TableRow.LayoutParams.MatchParent,TableRow.LayoutParams.WrapContent);
row.LayoutParameters=lp;
foreach(系列中的var系列1)
{
var b=新按钮(此按钮);
b、 Text=系列1.系列;
//lp=新的TableLayout.LayoutParams(1,LinearLayout.LayoutParams.WrapContent);
b、 LayoutParameters=lp;
行。添加视图(b);
i=+1;
如果(i>1)
{
AddView(行,新的TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent,TableLayout.LayoutParams.WrapContent));
行=新表行(此);
row.LayoutParameters=lp;
i=0;
}
}
有什么建议吗
提前感谢,
比约恩
我一直在尝试创建一个两个按钮相邻的表格布局
仅根据您的代码,除了名为按钮布局
的原始表格布局
,您还创建了另一个表格布局
和一个表格行
,然后将按钮
控件添加到此行中,但您从未将此行添加到表格布局
的视图中,您需要注意LayoutParams
,您对所有控件都使用了TableLayout.LayoutParams
,但其中是LinearLayout.LayoutParams
,这是不对的
由于我没有您的系列
,我无法真正理解您在foreach
中所做的工作,这里我只发布了一个示例代码:
TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout);
ll.WeightSum = 2;
ll.Orientation = Orientation.Horizontal;
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent);
row.LayoutParameters = lp;
var b = new Button(this);
b.Text = "Button1";
b.LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent);
row.AddView(b);
var bb = new Button(this);
bb.Text = "Button2";
bb.LayoutParameters = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent);
row.AddView(bb);
ll.AddView(row);
ll.Invalidate();
TableLayout ll=findviewbyd(Resource.Id.buttonLayout);
ll.WeightSum=2;
ll.方向=方向.水平;
TableRow row=新的TableRow(本);
TableRow.LayoutParams lp=新的TableRow.LayoutParams(TableRow.LayoutParams.MatchParent,TableRow.LayoutParams.WrapContent);
row.LayoutParameters=lp;
var b=新按钮(此按钮);
b、 Text=“Button1”;
b、 LayoutParameters=new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent,TableRow.LayoutParams.WrapContent);
行。添加视图(b);
var bb=新按钮(此按钮);
bb.Text=“按钮2”;
bb.LayoutParameters=新建TableRow.LayoutParams(TableRow.LayoutParams.MatchParent,TableRow.LayoutParams.WrapContent);
行。添加视图(bb);
ll.AddView(行);
ll.Invalidate();
我在分配一些不需要的东西。但我最愚蠢的错误是我做的那部分i=+1;而不是i+=1
这就是我的代码现在的样子。它正在做它现在需要做的事情
TableLayout ll = FindViewById<TableLayout>(Resource.Id.buttonLayout);
//TableLayout llInner = new TableLayout(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
//ll.Orientation = Orientation.Horizontal;
//ll.LayoutParameters = lp;
//ll.WeightSum = 2;
//ll.AddView(llInner);
var i = 0;
var row = new TableRow(this);
row.WeightSum = 2;
//TableRow.LayoutParams rowlayout = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.WrapContent);
//row.LayoutParameters = lp;
foreach (var series1 in series)
{
var b = new Button(this);
b.Text = series1.Series;
lp.Weight = 1;
//lp = new TableLayout.LayoutParams(1, LinearLayout.LayoutParams.WrapContent);
b.LayoutParameters = lp;
row.AddView(b);
i += 1;
if (i > 1)
{
ll.AddView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent, TableLayout.LayoutParams.WrapContent));
row = new TableRow(this);
row.LayoutParameters = lp;
i = 0;
}
TableLayout ll=findviewbyd(Resource.Id.buttonLayout);
//TableLayout Lliner=新的TableLayout(本);
TableRow.LayoutParams lp=新的TableRow.LayoutParams(LinearLayout.LayoutParams.MatchParent,LinearLayout.LayoutParams.MatchParent);
//ll.方向=方向.水平;
//ll.LayoutParameters=lp;
//ll.WeightSum=2;
//ll.AddView(LLINER);
var i=0;
var row=新表格行(此);
row.WeightSum=2;
//TableRow.LayoutParams rowlayout=新建TableRow.LayoutParams(TableRow.LayoutParams.MatchParent,TableRow.LayoutParams.WrapContent);
//row.LayoutParameters=lp;
foreach(系列中的var系列1)
{
var b=新按钮(此按钮);
b、 Text=系列1.系列;
lp.重量=1;
//lp=新的TableLayout.LayoutParams(1,LinearLayout.LayoutParams.WrapContent);
b、 LayoutParameters=lp;
行。添加视图(b);
i+=1;
如果(i>1)
{
ll.AddView(行,新建TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent,TableLayout.LayoutParams.WrapContent));
行=新表行(此);
row.LayoutParameters=lp;
i=0;
}
Allocate of stuff被注释了,因为我已经搞乱它有一段时间了。大家好,为什么不尝试使用.axml页面。在Xamarin traditional和Xamarin forms中,设计页面是opmitise。而且非常有用。因为需要创建的按钮数量是动态的,它基于我从API收到的信息。因此我不知道我需要创建多少个按钮。