Как добавить данные в макет таблицы с флажком в Xamarin Android - PullRequest
0 голосов
/ 15 января 2019

Как добавить данные в макет таблицы с помощью флажка в Xamarin android.

образец, как показано ниже:

enter image description here

1 Ответ

0 голосов
/ 16 января 2019

Я написал демо, которое добавляет данные в макет таблицы, вы можете обратиться к нему.

Это GIF демо.

enter image description here

Ниже приведен код. MainActivity.cs

namespace CheckBoxDemo
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    TableLayout tl_view;
    CheckBox cb;
    TextView tv_view1;
    TextView tv_view2;
    TextView tv_view3;
    TextView tv_view4;

    TableRow tb;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

         tl_view = FindViewById<TableLayout>(Resource.Id.tl_view);
        int count = 1;

        Button bt_add = FindViewById<Button>(Resource.Id.bt_add);
        bt_add.Click += (e, o) => {
            addRole(ref count);
        };
    }

    public void addRole( ref int count)
    {
         cb = new CheckBox(this);
         tv_view1 = new TextView(this);
        if (count < 10)
        {

            tv_view1.Text = "0"+count;

        }
        else
        {
            tv_view1.Text = "" + count;

        }
        count++;
        tv_view2 = new TextView(this);
        tv_view2.Text = "test";

        tv_view3 = new TextView(this);
        tv_view3.Text = "test";

        tv_view4 = new TextView(this);
        tv_view4.Text = "1";


        tb = new TableRow(this);
        tb.AddView(cb);
        tb.AddView(tv_view1);
        tb.AddView(tv_view2);
        tb.AddView(tv_view3);
        tb.AddView(tv_view4);
        tl_view.AddView(tb);
    }

}
}

activity_main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_layout ">

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tl_view "
    >
<TableRow>  

   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="   "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="  Col1             "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col2              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
   <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col3              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />
    <TextView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"
        android:text="Col4              "
        android:textColor="@android:color/black"
        android:textSize="15dp"
    />

</TableRow> 

</TableLayout>

<Button
 android:id="@+id/bt_add"
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"
 android:text="add"
/>
</LinearLayout>
...