【问题标题】:Column Span of GridLayout is not working in Xamarin.androidGridLayout 的列跨度在 Xamarin.android 中不起作用
【发布时间】:2020-10-30 06:33:55
【问题描述】:

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

问题是,我无法将 Button 放入 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);
        }

【问题讨论】:

    标签: android xamarin.android android-gridlayout


    【解决方案1】:

    代码可以将 Button 放入 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.
    

    【讨论】:

    • 谢谢.. 非常有用:)
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 2013-06-23
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多