在TableRow中居中ImageView

时间:2013-10-26 15:37:31

标签: android android-layout

我在TableLayout(TableRow)中居中图像时遇到问题。 ImageView总是在左侧,我需要让他居中。

这是XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff2d8">

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/logo"
                android:id="@+id/imageView"
            />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="10dp"
        android:layout_weight="1">
    <TableRow
          .
          .

有布局图片:screenshot

我该怎么做?非常感谢您的帮助。

此致,Petr

3 个答案:

答案 0 :(得分:3)

像这样更新您的布局

 <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <TableRow
            android:gravity="center_horizontal|center_vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ImageView
                android:id="@+id/imageView"
                android:background="@drawable/ic_launcher"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/logo" />
        </TableRow>
    </TableLayout>

答案 1 :(得分:2)

您可以将android:gravity="center"添加到TableRow的XML定义中。

答案 2 :(得分:0)

android:layout_span="2"

中尝试ImageView
<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/logo"
                android:id="@+id/imageView"
                android:layout_span="2"/>
        </TableRow>
    </TableLayout>