带有ImageView的TableLayout

时间:2019-03-19 16:34:47

标签: android android-layout

我是android的新手,正在学习。我正在尝试使我的活动布局如下图所示。 enter image description here

我想使用TableLayout进行如下操作

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:id="@+id/row_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="4dp"
        android:background="#ed8404">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:gravity="center"
            android:scaleType="fitCenter"
            android:src="@drawable/image1" />
    </TableRow>


    <TableRow
        android:id="@+id/row_2"
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:gravity="center"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/image1"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/image1" />

    </TableRow>


    <TableRow
        android:id="@+id/row_3"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_marginTop="4dp"
        android:background="#ed8404">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:adjustViewBounds="true"
            android:src="@drawable/image1"/>
    </TableRow>

    <TableRow
        android:layout_weight="1"
        android:id="@+id/row_4"
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:gravity="center"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/image1"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true"
            android:src="@drawable/image1" />

    </TableRow>
</TableLayout>

但是我无法在所有图像上固定适当的高度。它看起来像这张图片。

enter image description here

让我知道是否有人可以帮助我解决问题。我希望所有图像都适合以相同的高度查看。非常感谢。

1 个答案:

答案 0 :(得分:1)

如果元素过多,无法在屏幕上显示,则必须使用RecyclerView(使用不同的ViewHolder)。

如果元素的数量固定,则可以使用LinearLayout降低嵌套级别:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/table_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="#F0F"
        android:gravity="center"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#0FF"
            android:gravity="center"
            android:scaleType="fitCenter" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#FF0"
            android:gravity="center"
            android:scaleType="fitCenter" />

    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:background="#00F"
        android:gravity="center"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#0F0"
            android:gravity="center"
            android:scaleType="fitCenter" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="#F00"
            android:gravity="center"
            android:scaleType="fitCenter" />

    </LinearLayout>

</LinearLayout>

可以预期的输出: layout