выравнивание макета таблицы tablelayout

я совершенно не понимаю, почему test, test1, test2, test3 неправильно выравниваются с первой строкой. Первая строка относится к тесту, 2012, тесту, тесту. Я хочу, чтобы следующая строка следовала за первой строкой в ​​таблице. Пожалуйста помоги. Заранее спасибо.

XML

 <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tl"
        android:weightSum="5"
        >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#27160e" >

       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textStyle="bold"
            android:textSize="18sp"
            android:gravity="center"
            android:textColor="#fad06e"
            android:layout_weight="1"
            android:text="Project" />

       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textStyle="bold"
            android:textSize="18sp"
            android:gravity="center"
            android:textColor="#fad06e"
            android:layout_weight="1"
            android:text="Project2" />

         <TableLayout
             android:layout_weight="3"
        android:layout_marginRight="4dp"
       android:stretchColumns="*"
       >



       <TableRow>
       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="Cost"
            android:gravity="center"
            android:layout_span="2" />
   </TableRow>
        <TableRow
            >




       <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="2011" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="18sp"
            android:textColor="#fad06e"
            android:text="2012" />
                  </TableRow>
                  </TableLayout>
    </TableRow>

  <TableRow>
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="test" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:layout_weight="1"

            android:text="test" />
      <TableLayout android:stretchColumns="*"
          android:layout_weight="3">
          <TableRow>
                <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:text="test" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:text="test" />

          </TableRow>

      </TableLayout>

  </TableRow>



</TableLayout>

Код:

TableLayout tl;

        tl = (TableLayout) findViewById(R.id.tl);

        for(int x = 0; x < 3; x++) {

            // Create the table row
            TableRow tr = new TableRow(this);
            tr.setId(100+x);

            TextView lblrecID = new TextView(this);
            lblrecID.setId(200+x);
            lblrecID.setText("test");
            lblrecID.setGravity(Gravity.CENTER); 
            LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
            lblrecID.setLayoutParams(params);
            tr.addView(lblrecID);// add the column to the table row here

            TextView lblrecName = new TextView(this);
            lblrecName.setId(200+x);// define id that must be unique
            lblrecName.setText("test1");
            lblrecName.setGravity(Gravity.CENTER);
            lblrecName.setLayoutParams(params);
            tr.addView(lblrecName); // add the column to the table row here

            TableLayout tl1 = new TableLayout(this);
            LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f);
            tl1.setStretchAllColumns(true);
            tl1.setLayoutParams(params1);
            TableRow tr2 = new TableRow(this);

            TextView lblrecID1 = new TextView(this);
            lblrecID1.setId(200+x);
            lblrecID1.setText("test2");
            lblrecID1.setGravity(Gravity.CENTER); 
            tr2.addView(lblrecID1);// add the column to the table row here

            TextView lblrecName1 = new TextView(this);
            lblrecName1.setId(200+x);// define id that must be unique
            lblrecName1.setText("test3");
            lblrecName1.setGravity(Gravity.CENTER);
            tr2.addView(lblrecName1); // add the column to the table row here
            tl1.addView(tr2);     
            // finally add this to the table row

            tr.addView(tl1);
            //tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


        }

Результат введите здесь описание изображения


person micky    schedule 31.07.2014    source источник


Ответы (1)


В порядке. Я не понимаю, чего ты хочешь. Может это такая планировка?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*">

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#27160e" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Project"
        android:textColor="#fad06e"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Project2"
        android:textColor="#fad06e"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Cost"
        android:textColor="#fad06e"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="2011"
        android:textColor="#fad06e"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="2012"
        android:textColor="#fad06e"
        android:textSize="18sp"
        android:textStyle="bold" />
</TableRow>

<TableRow>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />
</TableRow>

 <TableRow>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="test" />
</TableRow>

person João Menighin    schedule 31.07.2014
comment
Отредактировал мой ответ. Пожалуйста, будьте более конкретными. Что вы хотите? Столбец Cost должен быть пустым что ли? - person João Menighin; 31.07.2014
comment
не правильное выравнивание. Может ли тест совпадать с тестовым столбцом, test1 совпадать со столбцом 2012 и так далее... - person micky; 31.07.2014