redrom
redrom

Reputation: 11642

Android Table row, cannot center text

i have following layout code:

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

    <!-- APP NAME -->
    <TableRow
        android:id="@+id/table_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#2d9adf"
        android:gravity="center_vertical"
        android:padding="5dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:gravity="center_horizontal"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:textSize="16sp" />

        </RelativeLayout>
    </TableRow>

Problem is, that result is not centered (see image below):

enter image description here

How can I solve it please?

Upvotes: 1

Views: 40

Answers (1)

Alex K
Alex K

Reputation: 8338

Very easy fix to this. You need to change your RelativeLayout to have the correct dimensions:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

That way it will stretch the full size of the TableRow at the top. Then you can add this to your TextView:

android:layout_centerInParent="true"

to center the TextView in the RelativeLayout, which is spanning the TableRow.

Upvotes: 2

Related Questions