0

那是我的标签。 我的标签

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

该表是从 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>

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

4

1 回答 1

0

change your layout XML to this

<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>
于 2013-02-06T12:45:43.240 回答