自定义对话框,将imageview导入gridview

时间:2016-03-04 11:15:11

标签: android gridview android-dialog

我有一个自定义对话框,我想要显示10个图标。 我有这个代码

{{1}}

如何制作一系列图标?目前我只能看到1,2,3,4,5 .... 有谁可以帮助我吗? 谢谢

2 个答案:

答案 0 :(得分:3)

创建自定义ArrayAdapter

    public class ImageAdapter extends ArrayAdapter {
    private Context mContext;

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

    // 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) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
    };
}

替换mThumbIds您的图标ID中的ID。之后,将适配器设置为GridView

gridview.setAdapter(new ImageAdapter(this));

调整图标大小:

//Width and height in pixels
int width = 150;
int height = 150; 
imageView.setLayoutParams(new GridView.LayoutParams(width, height));

答案 1 :(得分:1)

您需要为Gridview创建一个视图适配器,然后使用它设置图像。你可以用它作为参考 link