Adjust GridView to all Screen Sizes

前端 未结 1 998
长情又很酷
长情又很酷 2020-12-30 08:26

Im trying to implement a GridView as part of a image gallery. I followed the following example from the Android developer portal.

The tutorial seems to work for all

相关标签:
1条回答
  • 2020-12-30 08:35

    SOLVED:

    I solved this issue by editing the ImageAdapter of the GridView. I calculated the size of the generated image views - density independent.

    // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {  
    
                //Calculation of ImageView Size - density independent.
                //maybe you should do this calculation not exactly in this method but put is somewhere else.
                Resources r = Resources.getSystem();
                float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
    
    
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //imageView.setPadding(8, 8, 8, 8);
                imageView.setBackgroundColor(Color.BLUE);
            } else {
                imageView = (ImageView) convertView;
            }
    
            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }
    
    0 讨论(0)
提交回复
热议问题