如何在xamarin的表格布局中动态添加表格行?

问题描述 投票:0回答:1

我试图在一个表格布局中动态地添加一行,但当我试图使用下面的代码时,新的表格行没有显示出来,尽管代码运行时没有崩溃。然而,当我试图使用下面的代码时,新的表格行没有显示出来,尽管代码运行时没有崩溃。

这就是代码。

            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);
            }
c# xamarin tablelayout
1个回答
0
投票

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);
    }
© www.soinside.com 2019 - 2024. All rights reserved.