2011-06-08 18 views
19

イメージをAlertDialogに入れる方法がわかりません。AlertDialogに画像を挿入する方法は? Android

私はこのコードを持っていますが、これは不可能と思います。この

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(); 

答えて

54

sample.xmlを1つ作成し、その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(); 
+0

私にエラーが表示されます:アクティビティのメソッドを実行できませんでした – fr4n

+0

新しいコードを試しましたか? –

+0

はい、更新しました。私は画像とボタンを持つ新しいxml "sample.xml"を持っています。しかし、それは私にエラーを与える。 – fr4n

6

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(); 
18

次のようにすることができます。これは、(あなたがメッセージを必要としない場合は、単にその行を削除します)メッセージをalertDialogを表示し、画像(およびOKボタン)します:画像用の

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(); 
+5

代わりにメッセージを下部に移動するには? – user2872856

2

を私はちょうど

ITを行います

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

関連する問題