Search code examples
androidandroid-tablelayout

Can't center textview in tablelayout


I can't get the bottom row below to center, specifically the columns with integers.

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:padding="10dip" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/player_profile"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <TextView
                android:id="@+id/player_number"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/player_name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/statistics_row"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="1dip"
            android:gravity="center_horizontal">

            <TextView
                android:id="@+id/statistic_points"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/statistic_rebounds"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/statistic_assists"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/statistic_steals"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/statistic_blocks"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/statistic_turnovers"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_gravity="center_horizontal"
                android:layout_weight="1" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Solution

  • It's not clear which row you're unhappy with, but realize that there are 2 different gravity attributes. layout_gravity positions the view that it's defined in, while gravity positions items within that view. For layouts, it positions the views within it; for views such as TextViews it positions the text within the view.