【问题标题】:TableLayout not centered verticallyTableLayout 未垂直居中
【发布时间】:2013-12-15 23:50:30
【问题描述】:

我在RelativeLayout 中有一个TableLayout,其中android:layout_centerInParent="true" 作为其属性之一,但它没有垂直居中。

这是我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/line_drawing_relativelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableLayout 
        android:id="@+id/activity_linking_tl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <TableRow
            android:id="@+id/table_row_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="1dp" >

            <com.xx.myview
                android:id="@+id/id_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <com.xx.myview
                android:id="@+id/id_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>
    </TableLayout>

 <ImageButton
        android:id="@+id/line_drawing_b_restart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:padding="22dp"
        android:src="@drawable/ic_restart" >
    </ImageButton>

<!-- There are more views/widgets here -->

</RelativeLayout>

我将如何解决这个问题?

【问题讨论】:

    标签: android android-layout tablelayout android-tablelayout


    【解决方案1】:

    您确定 RelativeLayout 占用的空间与您想象的一样多吗?可能是 TableLayout 居中,但 RelativeLayout 正在包装内容,因此它最终居中但在比您预期的更小的容器中。如果没有看到 XML 的 RelativeLayout 部分,我很难判断。

    检查布局的一种方法是使用developer option“显示布局边界”:

    它会让您更好地了解您的布局是如何构建的。如果您使用的是没有此开发者选项的旧手机,我将开始为视图背景设置平面颜色(例如android:background="#F00")。

    【讨论】:

    • 我已经检查了RealtiveLayout 是否按照需要填满了整个屏幕。我将添加整个文件。
    【解决方案2】:

    尝试将父级 RelativeLayout 的宽度和高度设置为 match_parent。

    并将其添加到您的 TableLayout

    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    

    【讨论】:

    • 感谢您的回复,但没有帮助。我将添加整个布局文件。
    【解决方案3】:

    问题是我也有android:layout_below="xxx"(由于某种原因未在我发布的代码中显示),它覆盖了我试图实现的居中。删除它解决了它。确实很明显。

    【讨论】: