zoukankan      html  css  js  c++  java
  • Android应用程序当中GridView显示不同大小图片时Item挤出问题解决办法

    1、问题说明

            在开发GridView的时候有这样的一个需求,即从网络上下载相应的图片然后显示在GridView当中,显示效果如下:

           

            以上的图片,不同的图片是对齐的,没有图片挤出,太大或者太小的问题,做出这种效果的前提是,图片大小是一样的。但是,在实际情况下,图片的大小多数情况下是不一致的,就会造成如下的效果,



    图片大小不同,导致大的图片将下面的文字挤出GridView中的Item,解决方案很简单,只要将显示在GridView中的Item的ImageView重写一下,控制一下大小即可。这种方式,同本博客ScrollView嵌套GridView、ListView的解决办法类似。

    2、复写ImageView

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class SquareImageView extends ImageView {
    
        public SquareImageView(Context context) {
            super(context);
        }
    
        public SquareImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            // Snap to width
            setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
        }
    }

    将现有的SquareImageView代替原来的ImageView,

     <com.remind.view.SquareImageView
            android:id="@+id/img_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true" />

    3、修改后效果

    现在图片挤出的效果不见了,GridView当中每个Item大小都是一致的。

  • 相关阅读:
    协程基础及其创建和使用方法
    创建进程池与线程池concurrent.futures模块的使用
    线程队列queue的使用
    线程操作之锁的使用
    linux内核调试指南
    在开发板Linux上挂载"驱动"挂载不成功,出现提示server 172.27.52.100 not responding, still trying
    LPC1788 SDRAM运行程序
    Altium designer 原理图库快速创建
    NFS挂载启动
    网站记录
  • 原文地址:https://www.cnblogs.com/chenlong-50954265/p/5759860.html
Copyright © 2011-2022 走看看