当前位置: 移动技术网 > 移动技术>移动开发>Android > GridView中图片显示出现上下间距过大,左右图片显示类似瀑布流的问题

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

2020年07月23日  | 移动技术网移动技术  | 我要评论

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

我们可以通过不同的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中尽量放的图片格式大小相同,可以减少一些问题的出现。

本文地址:https://blog.csdn.net/kisaragi_yu/article/details/107445436

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网