如何在Android中向AlertDialog添加图片?

3

我创建了一个AlertDialog,其中有两个选项,一个是打开相机,另一个是打开相册。

如下图所示:
enter image description here

但我需要像这样添加相机和相册图标:

enter image description here

如何在代码中添加这些图片?

    final String [] items   = new String [] {"Take from camera", "Select from gallery"};    
      ArrayAdapter<String> adapter  = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
      AlertDialog.Builder builder   = new AlertDialog.Builder(this);

      builder.setTitle("Select Image");
      builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
      public void onClick( DialogInterface dialog, int item ) { //pick from camera
      if (item == 0) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

      mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
      "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

      try {
      intent.putExtra("return-data", true);

      startActivityForResult(intent, PICK_FROM_CAMERA);
      } catch (ActivityNotFoundException e) {
      e.printStackTrace();
      }
      } else { //pick from file
      Intent intent = new Intent();

      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);

      startActivityForResult(Intent.createChooser(intent,    "Complete               action               using"), PICK_FROM_FILE);
      }
      }
      });

      final AlertDialog dialog = builder.create();



      dialog.show();

}

您可以使用自定义对话框。http://www.mkyong.com/android/android-custom-dialog-example/ - Bhoomika Brahmbhatt
1个回答

6
按照您的意愿创建布局,然后将该布局添加到对话框中。
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF" 
        android:layout_toRightOf="@+id/image" />

   <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image" />

</RelativeLayout>

然后在活动中进行

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Bla Bla");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Your Text");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
    }
});

dialog.show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接