如何在AlertDialog中显示GridView中的图像?

时间:2014-02-06 06:35:49

标签: android imageview

mGridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> gridView, View view,
            int pos, long id) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(view);
        builder.create().show();        

    }
});

当我尝试运行此代码时,单击某个项目后,我得到一个例外:

enter image description here

我不想从其父级中删除视图。我只想在AlertDialog中显示图像。有人能给我一个提示怎么做吗?

2 个答案:

答案 0 :(得分:1)

试试这个......

    mGridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> gridView, View view,
                int pos, long id) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            ImageView imageView = new ImageView(gridView.getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap bitmap = view.getDrawingCache();
            imageView.setImageBitmap(bitmap);
            builder.setView(imageView);
            builder.create().show();
        }
    });

答案 1 :(得分:0)

如果您只想显示图像,则可以设置

builder .setIcon(R.drawable.ic_launcher);

然后你不需要setView。

删除此行:

builder.setView(view);