如何将 table 行动态添加到 xamarin 中的 table 布局

How to add a table row dynamically to a table layout in xamarin

我正在尝试向 table 布局中动态添加一行。然而,当我尝试使用下面的代码时,新的 table 行没有显示,尽管代码是 运行 而没有崩溃。

这是代码:

            tl1 = (TableLayout)FindViewById(Resource.Id.tl1);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent, (float)1.0);
            layoutParams.SetMargins(10, 0, 5, 0);
            TableRow.LayoutParams layoutParams2 = new TableRow.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);
            dbname = "eventsss";
            path = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), dbname);
            var db = new SQLiteConnection(path);
            db.CreateTable<CalendarEvent>();
            eventList = GetAllEvents();
            for (int i = 1; i <= eventList.Count; i++)
            {
                date = new TextView(this);
                date.LayoutParameters = layoutParams;
                date.SetBackgroundColor(Color.ParseColor("#FFFFC0"));
                date.SetTextSize(Android.Util.ComplexUnitType.Px, (float)30.0);
                date.SetTextColor(Color.Black);
                date.SetPadding(5, 5, 5, 5);
                name = new TextView(this);
                name.LayoutParameters = layoutParams;
                name.SetBackgroundColor(Color.ParseColor("#FFFFC0"));
                name.SetTextColor(Color.Black);
                name.SetTextSize(Android.Util.ComplexUnitType.Px, (float)30.0);
                name.SetPadding(5, 5, 5, 5);
                tr = new TableRow(this);
                tr.LayoutParameters=layoutParams2;
                name.Text = eventList[i-1].title;
                date.Text = eventList[i-1].date;
                tr.AddView(date);
                tr.AddView(name);
                tl1.AddView(tr, i);
            }

TableRow 应该使用 TableLayout.LayoutParams.

TableRow 内的视图应使用 TableRow.LayoutParams

在你的代码中,它应该是这样的:

    for (int i = 0; i <= 10; i++)
    {

        TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);

        TableRow.LayoutParams layoutParams2 = new TableRow.LayoutParams(LayoutParams.MatchParent, LayoutParams.WrapContent);

        TextView date = new TextView(this);

        //Use TableRow
        date.LayoutParameters = layoutParams2;

        date.SetBackgroundColor(Color.ParseColor("#FFFFC0"));
        date.SetTextSize(Android.Util.ComplexUnitType.Px, (float)30.0);
        date.SetTextColor(Color.Black);
        date.SetPadding(5, 5, 5, 5);

        TextView name = new TextView(this);

        //Use TableRow
        name.LayoutParameters = layoutParams2;

        name.SetBackgroundColor(Color.ParseColor("#FFFFC0"));
        name.SetTextColor(Color.Black);
        name.SetTextSize(Android.Util.ComplexUnitType.Px, (float)30.0);
        name.SetPadding(5, 5, 5, 5);
        TableRow tr = new TableRow(this);

        //Use TableLayout
        tr.LayoutParameters = layoutParams;

        name.Text = "title";
        date.Text = "date";
        tr.AddView(date);
        tr.AddView(name);
        tl1.AddView(tr, i);
    }