如何在 TableLayout 的中心添加图像

How to add a image at the center of a TableLayout

我有一个带有一些表格行的 TableLayout:

我想在上面的布局上的圆形标记下添加一张图片,如下所述:

这是我的布局代码:

   <TableLayout
        android:layout_width="wrap_content"
        android:id="@+id/table1"
        android:gravity="center"
        android:layout_height="wrap_content">

        <TableRow>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/img1_1"
        android:background="@drawable/button"
        android:layout_margin="3dp"
        />

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/img1_2"
        android:background="@drawable/button"
        android:layout_margin="3dp" />

    </TableRow>
    <TableRow>

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/img1_3"
        android:background="@drawable/button"
        android:layout_margin="3dp"
   />

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/img1_4"
        android:background="@drawable/button"
        android:layout_margin="3dp" />
    </TableRow>

</TableLayout>

那么如何在这个布局的中心位置添加图片呢?

试试这个结构

<FrameLayout>
<TableLayout></TableLayout>
<ImageView android:layout_gravity="centre"/>
</FrameLayout>

像这样的东西会起作用。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="biw.myapplication.MainActivity">

<TableLayout
    android:layout_width="wrap_content"
    android:id="@+id/table1"
    android:layout_gravity="center"
    android:layout_height="wrap_content">

    <TableRow>

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/img1_1"
            android:background="@drawable/button"
            android:layout_margin="3dp"
            />

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/img1_2"
            android:background="@drawable/button"
            android:layout_margin="3dp" />

    </TableRow>
    <TableRow>

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/img1_3"
            android:background="@drawable/button"
            android:layout_margin="3dp"
            />

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:id="@+id/img1_4"
            android:background="@drawable/button"
            android:layout_margin="3dp" />
    </TableRow>
</TableLayout>

<ImageView
    android:layout_gravity="center"
    android:layout_width="70dp"
    android:layout_height="70dp" />

</FrameLayout>

值得注意的是,您应该查看 gravitylayout_gravity 之间的区别,gravity 分配小部件在布局中的对齐方式,而 layout_gravity 分配与布局本身的对齐。

此外,此代码仅在您为图像分配静态宽度和高度时才有效。

结果: