Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 对话框中的图像_Android_Imageview_Android Dialog - Fatal编程技术网

Android 对话框中的图像

Android 对话框中的图像,android,imageview,android-dialog,Android,Imageview,Android Dialog,我试图在一个对话框中显示一个ImageView,我遵循了几个示例,但没有一个像我打开对话框时应用程序关闭时那样工作 <ImageView android:id="@+id/image" android:contentDescription="@string/desc1" android:layout_width="wrap_content" android:layout_height="wrap_content" />

我试图在一个对话框中显示一个ImageView,我遵循了几个示例,但没有一个像我打开对话框时应用程序关闭时那样工作

<ImageView android:id="@+id/image"
           android:contentDescription="@string/desc1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
使用这个新的代码,我已经能够显示对话框内的图像,我也能够旋转图像

   button1.setOnLongClickListener(new OnLongClickListener()
    {
        public boolean onLongClick(View v)
        {
            Dialog dialog = new Dialog(MainActivity.this);
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);


            RotateAnimation anim = new RotateAnimation(0f, 360f, 200f, 200f);
            anim.setInterpolator(new LinearInterpolator());
            anim.setRepeatCount(Animation.INFINITE);
            anim.setDuration(10000);

            dialog.setTitle("You have found the easter egg!");
            View view = inflater.inflate(R.layout.activity_main2, null);
            dialog.setContentView(view);
            view.startAnimation(anim);

            dialog.show();
            return false;

        }


    });

}

您不应将AlertDialog用于自定义对话框。 使用常规对话框类及其setContentView()方法。也可以使用DialogFragment

UPD:我被告知AlertDialog有setView()方法。你可以试试

when I open the dialog the app closes. 
因为您的对话框
ImageView img
null

ImageView img = (ImageView) dialog.findViewById(R.id.image); // <--- img is not visible through dialog..
img.setImageResource(R.drawable.dust); // <--- this line throw exception

ImageView img=(ImageView)对话框。findviewbyd(R.id.image);//查看如何创建自定义对话框ImageView可用的xml布局文件名是什么???xml布局文件是Activity_main新代码与我的答案相同。不是吗???对不起-1。。是我。。您可以在AlertDailog中使用setView()。什么不起作用?例外?或者图像没有出现?请用新代码更新帖子。如果我们的图片资源是一个动画列表,那么如何制作图片动画呢
ImageView img = (ImageView) dialog.findViewById(R.id.image); // <--- img is not visible through dialog..
img.setImageResource(R.drawable.dust); // <--- this line throw exception
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).create();
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
dialog.setTitle("Title");
View view = inflater.inflate(R.layout.<xml_image>, null); // xml Layout file for imageView
ImageView img = (ImageView) view.findViewById(R.id.image);
img.setImageResource(R.drawable.dust);
dialog.setView(view);
dialog.show();