如何将标签对齐到android中心?

时间:2013-02-06 12:38:03

标签: android android-tablelayout

这是我的标签。 my tab

我想将它与屏幕中心对齐。 (水平和垂直)

该表是从java动态创建的。

我尝试过设置

TableLayout t1 = (TableLayout)findViewById(R.id.maintable);
t1.setGravity(Gravity.CENTER);

但它只是从顶部开始对齐。

public class TabActivity extends Activity {

Trny t = Trny.getInstance();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab);

    TableLayout t1 = (TableLayout)findViewById(R.id.maintable);
    t1.setGravity(Gravity.CENTER);

        // first row of table   

        TableRow tr = new TableRow(this);

        TextView place = new TextView(this);
        poradi.setText("P"  );
        poradi.setPadding(10, 5, 10, 5);
        tr.addView(place);

        TextView nameOfTeam = new TextView(this);
        nazTymu.setText("Nazev"  );
        nazTymu.setPadding(10, 5, 10, 5);
        tr.addView(nameOfTeam);

        TextView matchesPlayed = new TextView(this);
        odehraneZapasy.setText("Z" );
        odehraneZapasy.setPadding(10, 5, 10, 5);
        tr.addView(matchesPlayed);

        TextView numbefOfWins = new TextView(this);
        pocetVyher.setText("V" );
        pocetVyher.setPadding(10, 5, 10, 5);
        tr.addView(numbefOfWins);

        TextView numberOfTies = new TextView(this);
        pocetRemiz.setText("R" );
        pocetRemiz.setPadding(10, 5, 10, 5);
        tr.addView(numberOfTies);

        TextView numberOfLoses = new TextView(this);
        pocetProher.setText("P" );
        pocetProher.setPadding(10, 5, 10, 5);
        tr.addView(numberOfLoses);

        TextView goalsScored = new TextView(this);
        skorePlus.setText("VG" );
        skorePlus.setPadding(10, 5, 10, 5);
        tr.addView(goalsScored);

        TextView goalsGained = new TextView(this);
        skoreMinus.setText("OG" );
        skoreMinus.setPadding(10, 5, 10, 5);
        tr.addView(goalsGained);

        TextView numberOfPoints = new TextView(this);
        pocBodu.setText("B" );
        pocBodu.setPadding(10, 5, 10, 5);
        tr.addView(numberOfPoints);


        t1.addView(tr);

                 //more rows dynamically created from java

}

 }

XML文件

            <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"   
            android:id="@+id/maintable" >
            </TableLayout>

我怎样才能做到这一点?谢谢你们。

1 个答案:

答案 0 :(得分:0)

将布局XML更改为此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

        <TableLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   
        android:id="@+id/maintable"
        android:layout_centerInParent="true" >
        </TableLayout>
</RelativeLayout>