用户登录
用户注册

分享至

GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题

  • 作者: 段友38605
  • 来源: 51数据库
  • 2021-09-03

这种问题的首要原因是放置的图片格式大小不同。

我们可以通过不同的item布局观察其中的变化
GridVIew的layout如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <GridView
        android:id="@+id/grid_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2"
        />

首先贴一下正常效果:

1.wrap_content
这种必会出问题

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

效果就是下图,整的跟瀑布流一样

2.仅设置item中ImageView的scaleType
根据不同的设置,有不同的表现。
fitcenter 无变化
fitXY|center|centerCrop 间距问题能够解决,但同时图片会变形且item长度过长

3.设置ImageView为固定长度300dp

显示正常。

结论:我们从正常效果图可以看到,第二张图片的宽度与其他图片有显著区别。
因此,在GridView中尽量放的图片格式大小相同,可以减少一些问题的出现。

软件
前端设计
程序设计
Java相关