Я новый для xamarin, и я пытаюсь создать макет таблицы в своем основном действии, но я не хочу создавать его из xml. У меня есть номер, и я хочу создать таблицу на основе этого числа. если число можно разделить на 2, я хочу, чтобы в каждой строке было 2 столбца. else, я хочу создать строки, которые в каждой строке будут 2 colums, а в последней строке будет только один colum. извините за плохой английский. thnx!Создайте tablelayout программно
-2
A
ответ
1
Во-первых, создать свой основной макет (Main.axml), как это:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px" />
Тогда в ваших MainActivity.cs сделать это:
LinearLayout mainLayout = FindViewById<LinearLayout>(Resource.Id.mainLayout); //get your linearlayout from your Main.axml
TableLayout table = new TableLayout(this); //create a new tablelayout
mainLayout.AddView(table); //add your tablelayout to your mainlayout
int number = 19;
for (int i = 0; i < number; i++)
{
TableRow row = new TableRow(this); //create a new tablerow
row.SetGravity(GravityFlags.Center); //set it to be center (you can remove this if you don't want the row to be in the center)
TextView column1 = new TextView(this); //create a new textview for left column
TextView column2 = new TextView(this); //create a new textview for right column
if (number % 2 == 0) //if your number is even
{
column1.Text = "Details " + ++i; //insert text in the first textview
column2.Text = "Details " + (i + 1); //insert text in the second textview
row.AddView(column1); //add the first textview to your tablerow (left column)
row.AddView(column2); //add the second textview to your tablerow (right column)
table.AddView(row); //add the tablerow to your tablelayout
}
else //if your number is odd
{
column1.Text = "Details " + ++i;
row.AddView(column1);
if (i != number) //if it is not the last item, add another (right) column
{
column2.Text = "Details " + (i + 1);
row.AddView(column2);
}
table.AddView(row);
}
}
Я не эксперт в Xamarin. андроид, но я надеюсь, что это ответит на ваш вопрос. Хорошего дня.
С уважением, AziziAziz