3

我创建了一个 AlertDialog,它有两个选项,一个是打开相机,另一个是画廊

如图所示
在此处输入图像描述

但我需要像这样添加相机和画廊图像

在此处输入图像描述

如何添加图像是这段代码

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

}
4

1 回答 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();
于 2013-07-04T06:01:28.010 回答