如何在AlertDialog中放置图像? Android的

我不知道如何把一个图像放入AlertDialog。

我有这个代码,但我认为这是不可能的。

AlertDialog.Builder alert = new AlertDialog.Builder(MessageDemo.this); ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageResource(R.drawable.cw); alert.setView(imageView); alert.setNeutralButton("Here!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }); alert.show(); 

Solutions Collecting From Web of "如何在AlertDialog中放置图像? Android的"

创build一个sample.xml并在该XML中添加ImageView

sample.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/dialog_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout> 

Java代码:

 AlertDialog.Builder alertadd = new AlertDialog.Builder(MessageDemo.this); LayoutInflater factory = LayoutInflater.from(MessageDemo.this); final View view = factory.inflate(R.layout.sample, null); alertadd.setView(view); alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dlg, int sumthin) { } }); alertadd.show(); 

你可以这样做。 这将显示一个带有消息的alertDialog(如果您不需要该消息,只需删除该行)和图像(以及一个OKbutton):

 ImageView image = new ImageView(this); image.setImageResource(R.drawable.YOUR_IMAGE_ID); AlertDialog.Builder builder = new AlertDialog.Builder(this). setMessage("Message above the image"). setPositiveButton("OK", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }). setView(image); builder.create().show(); 

还有另外一个选项,你可以把图像放在alert对话框中,而不需要创build一个xml文件。

  AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this); builder.setCancelable(true); builder.setIcon(R.drawable.your image name); builder.setTitle("Incoming Call"); builder.setInverseBackgroundForced(true); builder.setPositiveButton("Accept",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("Reject",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog alert=builder.create(); alert.show(); 

检查这个: http : //www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

可能会对你有所帮助。

对于Image我只是做IT

public void onClick (View view){ new AlertDialog.Builder(this).setView (R.layout.your_layout).show();