简体   繁体   English

TableLayout不垂直居中

[英]TableLayout not centered vertically

I have a TableLayout inside a RelativeLayout with android:layout_centerInParent="true" as one of its attributes, but it is not being centered vertically. 我在RelativeLayout有一个TableLayout其中android:layout_centerInParent="true"作为其属性之一,但它没有垂直居中。

Here is my xml: 这是我的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>

How would I solve this? 我该如何解决?

Are you sure that the RelativeLayout is taking up as much space as you think? 您确定RelativeLayout占用了您认为的最大空间吗? It might be that the TableLayout is centered, but that the RelativeLayout is wrapping content, so it ends up being centered but in a smaller container than you expected. 可能是TableLayout是居中的,但RelativeLayout是包装内容,因此最终它居中,但是在比您期望的小的容器中。 It's hard for me to gauge without seeing the RelativeLayout portion of the XML. 对于我来说,很难看到XML的RelativeLayout部分。

One way to check your layouts is to use the developer option "show layout bounds": 检查布局的一种方法是使用开发人员选项 “显示布局范围”:

启用了“显示布局范围”的开发人员选项

It will give you a better idea of how your layouts are being constructed. 它将使您更好地了解如何构造布局。 If you are running an older phone without this developer option, I would just start setting flat colors for View backgrounds (eg android:background="#F00" ). 如果您运行的是没有此开发人员选项的旧手机,那么我将开始为View背景设置单色(例如android:background="#F00" )。

Try setting parent RelativeLayout width and height to match_parent. 尝试将父级RelativeLayout的宽度和高度设置为match_parent。

and add this to your TableLayout 并将其添加到您的TableLayout

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

The problem was that I had android:layout_below="xxx" as well (for some reason not shown in the code I posted), which overrode the centering I was trying to achieve. 问题是我也有android:layout_below="xxx" (出于某些原因,我发布的代码中未显示),这覆盖了我试图实现的居中。 Removing this solved it. 删除此解决了。 Obvious really. 确实很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM