如何在Android对话框中设置ImageView?

3

我想在Android对话框中显示一张图片。如果我从相册应用程序中选择了一张图片,那么它应该显示在对话框中。我已经尝试从相册应用程序中获取所选的图片,并将其路径传递给警告对话框。


这类与程序有关的内容,应该如何将其翻译成中文呢? - Vigneshwaran T
你只想知道如何在警告对话框中设置图像吗?还是也想知道如何从相册获取图像? - Shashank Udupa
我知道如何从相册中获取图像,我想知道如何在警告框中设置所选图像。@ShashankUdupa - Vigneshwaran T
显示 display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int screenWidth = size.x; int screenHeight = size.y; Bitmap bitmap = BitmapFactory.decodeFile(imagepath); int bitmapHeight = bitmap.getHeight(); int bitmapWidth = bitmap.getWidth(); BitmapDrawable resizedBitmap = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false)); - Vigneshwaran T
Dialog dialog = new Dialog(this); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.imageindialog); ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage); image.setBackground(Drawable.createFromPath(selectedImagePath)); dialog.getWindow().setBackgroundDrawable(null); dialog.show(); - Vigneshwaran T
显示剩余2条评论
4个回答

10
AlertDialog.Builder ImageDialog = new AlertDialog.Builder(MainActivity.this);
ImageDialog.setTitle("Title");
ImageView showImage = new ImageView(MainActivity.this);
ImageDialog.setView(showImage);

ImageDialog.setNegativeButton("ok", new DialogInterface.OnClickListener() 
{
    public void onClick(DialogInterface arg0, int arg1) 
    {   
    }
});
ImageDialog.show();

是的!这个解决方案是我见过的最简单的,而且很好用。 - Wookie

4

嘿,请跟随这个链接

首先,您需要将自定义对话框布局设置为对话框框中,如下所示:

setcontentview(R.layout.custom) 并使用setImageResources(your image id)设置图像。

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

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

我会尝试上面的示例,但我需要设置来自图库而不是drawable的图像。 - Vigneshwaran T
请清楚地告诉我,就像在 WhatsApp 中一样,你想要什么..@VigneshwaranT - Narendra Baratam
我正在开发聊天应用程序。我正在尝试实现用户想要向另一个用户发送图像的情况,用户从图库中选择图像,这一点我已经完成了。如果用户从图库中点击图像,则所选图像将显示在警报对话框中,并显示两个按钮,如“发送”和“取消”。@Narendra Baratam - Vigneshwaran T
哦,明白了。@VigneshwaranT - Narendra Baratam
2
AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater(); View dialogView = inflater.inflate(R.layout.imageindialog, null); ImageView imageView1 = (ImageView)dialogView.findViewById(R.id.goProDialogImage); imageView1.setImageBitmap(BitmapFactory.decodeFile(imagepath)); builder.setView(dialogView); - Vigneshwaran T
显示剩余3条评论

4

要在警告对话框中设置图像,您需要创建一个自定义对话框,如下所示:

dialog.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"
android:orientation="vertical">

      <ImageView
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:id="@+id/my_image"/>

</LinearLayout>

然后在您的活动中像这样显示您的自定义对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
                    .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create().show();

1

enter image description here

我使用这个简单的方法来显示AlertDialog:

我使用这个简单的方法来显示 AlertDialog

private void showAlertDialog(Context mContext, String mTitle, String mBody, int mImage){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setCancelable(true);
        builder.setIcon(mImage);
        if(mTitle.length()>0)
            builder.setTitle(mTitle);
        if(mBody.length()>0)
            builder.setTitle(mBody);

        builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }

调用方法:
showAlertDialog(mContext, "OOPS!", getString(R.string.massage_nointernet), R.drawable.ic_no_internet);

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