GridLayout 的列跨度在 Xamarin.android 中不起作用

Column Span of GridLayout is not working in Xamarin.android

我创建了一个动态指定其行和列的 GridLayout,之后我尝试在其中添加一个也是动态创建的按钮。

问题是,我无法将按钮放入 2 列范围内。它只填充了一列,即使我将列跨度设为 2。

下面是我试过的代码。请帮忙,我卡住了。

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    
        RelativeLayout mainlayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutparameter = new 
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MatchParent, 
        RelativeLayout.LayoutParams.MatchParent);
        mainlayout.LayoutParameters = layoutparameter;
    
        GridLayout gl = getView();
    
        mainlayout.AddView(gl);
    
        SetContentView(mainlayout);
    }

 private GridLayout getView()
        {

            GridLayout gridlayout = new GridLayout(this);

            Button b = new Button(this);
            b.Text = "Button1";


            var p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,
            ViewGroup.LayoutParams.MatchParent);
            gridlayout.LayoutParameters = p;

            gridlayout.RowCount = 2;
            gridlayout.ColumnCount = 2;

            addViewToGridLayout(gridlayout, b, 0, 1, 0, 2);// Here the column span given as 2.

            return gridlayout;
        }
    
 private void addViewToGridLayout(GridLayout pGridLayout, View view, int row, int pRowSpan, int column, int pColumnSpan)
        {
            Spec rowSpan = GridLayout.InvokeSpec(row, pRowSpan);
            Spec colspan = GridLayout.InvokeSpec(column, pColumnSpan);
            GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                    rowSpan, colspan);
            pGridLayout.AddView(view, gridParam);
        }

代码可以在 2 列跨度内容纳按钮。但是,如果列或行中没有任何内容,则不会进行更改。

使用 getView 方法进行更改。

 private GridLayout getView()
    {

        GridLayout gridlayout = new GridLayout(this);

        TextView textView1 = new TextView(this);
        textView1.Text = "csll0";

        TextView textView2 = new TextView(this);
        textView2.Text = "csll1";

        TextView textView3 = new TextView(this);
        textView3.Text = "csll3";

        TextView textView4 = new TextView(this);
        textView4.Text = "csll4";

        Button b = new Button(this);
        b.Text = "Button1";


        var p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent,
        ViewGroup.LayoutParams.MatchParent);

        gridlayout.LayoutParameters = p;

        gridlayout.RowCount = 3;
        gridlayout.ColumnCount = 2;

        addViewToGridLayout(gridlayout, textView1, 0, 1, 0, 1);
        addViewToGridLayout(gridlayout, textView2, 0, 1, 1, 1); 
        addViewToGridLayout(gridlayout, textView3, 1, 1, 0, 1); 
        addViewToGridLayout(gridlayout, textView4, 1, 1, 1, 1);
        addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.


        return gridlayout;
    }

将列跨度设置为 1。

 addViewToGridLayout(gridlayout, b, 2, 1, 0, 1); 

将列跨度设置为 2。

 addViewToGridLayout(gridlayout, b, 2, 1, 0, 2);// Here the column span given as 2.