在使用imageView时,gridView高度太高

ryc*_*ung 6 android android-imageview android-gridview

我对GridView中单元格的高度有问题.我的每个单元格都会有一个imageView,如下所示:

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

<GridView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:id="@+id/eventGridView" android:numColumns="2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是每个单元格的ImageView

<com.example.scheduling_android.view.RoundedCornerImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:maxWidth="150dp"
        android:maxHeight="150dp"
        xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/eventImageView"
        android:adjustViewBounds="false"/>
Run Code Online (Sandbox Code Playgroud)

我正在为我的imageView玩不同的图像大小.我得到的是,每当我的图像变大(例如,400 x 400px)时,单元格的高度变得非常高(大约是宽度的两倍,请注意我将网格设置为使用2列).但是,当我将图像保存为较小的尺寸(例如,160 x 160px)时,gridView正确显示.高度和宽度匹配.

这是一个截图:

在此输入图像描述

什么似乎是问题?可能是大图像导致gridView错误地计算其单元格的高度?我该怎么做才能解决这个问题?

Hal*_*lil 17

我认为你没有将adjustViewBounds设置为true.你能设置如下吗?

imageView.setAdjustViewBounds(true);
Run Code Online (Sandbox Code Playgroud)


ank*_*hoi 0

使用wrap_content而不是使用fill_parent.

因为 fill_parent 实际上会获取图像的原始高度并尝试将其填充到屏幕中。即使您使用 400x400 图像并设置 layout_width = "160dp"layout_height = "160dp",它也会起作用,因为我们不允许图像显示超过这个值。