1

I want to center a RelativeLayout which has a TableLayout inside of it INSIDE a LinearLayout. Hence the image below: enter image description here

Here is my XML code atm:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/titlebar_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    >
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/titlebar_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >

        <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/menu_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/foregroundColor">

            <TableRow>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" 
                    android:text="MEDIA"
                    android:textColor="@color/menuFontColor"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" 
                    android:text="CALENDER"
                    android:textColor="@color/menuFontColor"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" 
                    android:text="HOME"
                    android:textColor="@color/menuFontColor"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" 
                    android:text="NEXT EVENT"
                    android:textColor="@color/menuFontColor"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center" 
                    android:text="ABOUT"
                    android:textColor="@color/menuFontColor"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    />

            </TableRow>
        </TableLayout>
    </RelativeLayout>
</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Problem: The RelativeLayout refuses to center itself, it's aligns automaticlly to the left. Anyway to fix this?

4

1 回答 1

3

add android:gravity="center_horizontal" to your TableRow tag, this should do the trick

于 2012-11-23T09:06:14.427 回答