【问题标题】:Android GridView outside the margins边缘外的Android GridView
【发布时间】:2016-04-23 16:16:22
【问题描述】:

我在 gridview 中包含了一些图像,在滚动结束时,似乎无缘无故地超出了边缘。 (见图)谁能告诉我为什么?我发布了 XML 文件和图像适配器。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<GridView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/gridView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:numColumns="4"
    android:layout_marginBottom="20dp"/>

public class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context c) {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return imageName.length;
    }

    //---returns the ID of an item---
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageName[position]);
        return imageView;
    }
}

【问题讨论】:

    标签: android image gridview


    【解决方案1】:

    This 应该会有所帮助。

    基本上,更改此行以避免对像素大小进行硬编码。

    imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
    

    上面的答案here会告诉你如何获取当前的屏幕尺寸(高度/宽度)。使用这些值而不是 185。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2021-01-19
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      相关资源
      最近更新 更多