如何在android中以编程方式创建GridView图库?

时间:2014-03-13 07:50:20

标签: android gridview

我想以编程方式创建3x3 gridview以显示图像。我想通过获取屏幕尺寸来设置每个项目的高度和宽度。像:

DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

然后每个项目width = screenWidth/3;

请有人帮助我或某些链接以供参考。

2 个答案:

答案 0 :(得分:13)

这是您以编程方式创建gridview的方法,

    GridView grid = new GridView(this);
    grid.setId(ViewIdentification.getId());
    grid.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    grid.setBackgroundColor(Color.WHITE);
    grid.setNumColumns(3);
    grid.setColumnWidth(GridView.AUTO_FIT);
    grid.setVerticalSpacing(5);
    grid.setHorizontalSpacing(5);
    grid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);

将上述视图添加到您的布局中。在这里你可以得到显示器的高度和宽度。

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
            .getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int height = displaymetrics.heightPixels;

这是适配器类:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Bitmap[]mis_fotos;

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

    public int getCount() {
        return mis_fotos.length;
        }

    public Object getItem(int position) {
        return position;    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(width/3, height/3));
            imageView.setScaleType(ImageView.setScaleType(ScaleType.FIT_XY));
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageBitmap(mis_fotos[position]);
        return imageView;
    }
}

然后,这取决于您,如果您要传递动态URL,请相应地更改您的适配器。 将适配器设置为gridview。 如果您有任何问题,请告诉我。

答案 1 :(得分:2)

在您的活动中:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridView gridView = new GridView(this);
gridView.setNumColumns(3);
setContentView(gridView);
gridView.setAdapter(new GridViewAdapter(this));
}

和适配器:

private class GridViewAdapter extends BaseAdapter {
private Context context;
private final int length = 9;
public GridViewAdapter(Context context) {
            this.context = context;
}
@Override
        public int getCount() {
            return length;
        }
@Override
        public Object getItem(int position) {
            return null;
        }
@Override
        public long getItemId(int position) {
            return 0;
        }
@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(context);
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                int width = metrics.widthPixels / 3;
                int height = metrics.heightPixels / 3;
                imageView.setLayoutParams(new GridView.LayoutParams(width, height));
            } else {
                imageView = (ImageView) convertView;
            }
imageView.setImageResource(R.drawable.ic_launcher);
            return imageView;
        }
    }