8

我正在尝试构建类似标题的东西。我尝试在 TextView 上使用android:gravity="center"and android:textAlignment="center",但它没有使文本居中。我不知道问题出在哪里。

<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>
4

4 回答 4

17
<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>

于 2013-10-30T09:21:53.917 回答
17

android:gravity="center"TableRow.

于 2013-10-30T09:23:40.550 回答
2

将重力设置为表格行

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

           <TableRow 
                android:gravity="center_horizontal"/>
</TableLayout>
于 2013-10-30T10:20:36.260 回答
0

如果你有很多TextView内部TableRow这样做解决了我的问题

    <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/feedback" />

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/football" />

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/about" />

        </TableRow>
于 2019-01-14T07:54:47.113 回答