【问题标题】:xamarin creating a tablelayout with 2 buttons on each rowxamarin 创建一个表格布局,每行有 2 个按钮
【发布时间】:2017-10-05 15:54:24
【问题描述】:

我一直在尝试创建一个带有 2 个按钮的表格布局。我一直在阅读,但我似乎无法让它在屏幕上显示任何内容。这是我当前的 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;
            }
        }

有什么建议吗?

提前致谢,

比约恩

【问题讨论】:

  • 您好,您为什么不尝试使用 .axml 页面。在 Xamarin 传统和 Xamarin 表单设计页面是优化的。而且非常有用。
  • sinds 需要创建的按钮数量是动态的,它基于我从 API 收到的信息。因此我不知道我需要创建多少个按钮。

标签: c# android xamarin tablelayout


【解决方案1】:

我一直在尝试创建一个带有 2 个按钮的表格布局。

仅从您的代码中,除了名为 buttonLayout 的原始 TableLayout 之外,您还创建了另一个 TableLayout 和一个 TableRow,然后将 Button 控件添加到此行中,但您从未将此行添加到查看您的TableLayout,您需要注意LayoutParams,您对所有控件都使用了TableLayout.LayoutParams,但里面是LinearLayout.LayoutParams,这是不对的。

由于我没有你的series,所以我无法真正理解你在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();

【讨论】:

    【解决方案2】:

    我做了很多不需要的东西。但我最愚蠢的错误是我做的部分 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;
                    }
    

    很多东西都被评论了,因为我已经搞砸了一段时间了。

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 2016-11-30
      • 2018-08-15
      • 1970-01-01
      • 2023-01-12
      • 2011-01-18
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多