有没有人可以向我解释Xamarin表格中的rowspan和columnspan?右,左,上,下参数有点令人困惑。
让我们将这段代码用于rowspan:
grid.Children.Add(new Label
{
Text = "Span two rows (or more if you want)",
TextColor = Color.Yellow,
BackgroundColor = Color.Navy,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center
}, 2, 3, 1, 3);
数字2,3,1,3在列和行方面的含义是什么?这适用于具有四行三列的网格。
答案 0 :(得分:19)
此答案由Till Balandat从this answer at Xamarin's forums复制并粘贴,但在此处很有用,因为Xamarin的文档似乎没有解释Add方法的其他参数。
Add的重载需要4个参数,这有点令人困惑,但最后可以让你定义Row,RowSpan,Column和Columnspan: 所以上面的例子
var label = new Label { Text = "Row 1" };
myGrid.Children.Add(label,0,0);
Grid.SetColumnSpan(label,2);
转换为:
var label = new Label { Text = "Row 1" };
myGrid.Children.Add(label, 0, 2, 0, 1);
这就是Xamarin在内部所做的事情:
public void Add(View view, int left, int right, int top, int bottom)
{
//..exceptionhandling removed
Grid.SetRow((BindableObject) view, top);
Grid.SetRowSpan((BindableObject) view, bottom - top);
Grid.SetColumn((BindableObject) view, left);
Grid.SetColumnSpan((BindableObject) view, right - left);
this.Add(view);
}
所以你可能会对这样的事情感到更舒服:
public static class GridExtension
{
public static void AddChild(this Grid grid, View view, int row, int column, int rowspan = 1, int columnspan = 1)
{
if (row < 0)
throw new ArgumentOutOfRangeException("row");
if (column < 0)
throw new ArgumentOutOfRangeException("column");
if (rowspan <= 0)
throw new ArgumentOutOfRangeException("rowspan");
if (columnspan <= 0)
throw new ArgumentOutOfRangeException("columnspan");
if (view == null)
throw new ArgumentNullException("view");
Grid.SetRow((BindableObject)view, row);
Grid.SetRowSpan((BindableObject) view, rowspan);
Grid.SetColumn((BindableObject) view, column);
Grid.SetColumnSpan((BindableObject) view, columnspan);
grid.Children.Add(view);
}
}