Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a TextView in a TableRow in a TableLayout

Tags:

android

xml

I am trying to build something like a header. I tried using android:gravity="center" and android:textAlignment="center" on the TextView but it doesn't center the text. I don't know where the problem is.

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#008000" >

    <TextView
        android:id="@+id/HeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        <!--android:textAlignment="center"
        android:gravity="center"-->
        android:text="@string/header"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF"
        android:textStyle="italic" />

</TableRow>

</TableLayout>
like image 818
Marco Lau Avatar asked Oct 30 '13 09:10

Marco Lau


3 Answers

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="#008000" >

    <TextView
        android:id="@+id/HeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="header"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF"
        android:textStyle="italic" />
</TableRow>

like image 192
Shivang Trivedi Avatar answered Sep 20 '22 06:09

Shivang Trivedi


give android:gravity="center" to TableRow.

like image 25
KDeogharkar Avatar answered Sep 18 '22 06:09

KDeogharkar


Set gravity to table row

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

           <TableRow 
                android:gravity="center_horizontal"/>
</TableLayout>
like image 23
Query Avatar answered Sep 21 '22 06:09

Query