如何将图像放入 AlertDialog 中?安卓

发布于 11-14 23:08 字数 477 浏览 3 评论 0原文

我不知道如何将图像放入 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();

I don't know how to put an image into an AlertDialog.

I have this code, but i think this is not possible.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

放血2024-11-21 23:08:36

创建一个 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();

Create one sample.xml and add ImageView in that XML.

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 Code :

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();
画尸师2024-11-21 23:08:36

您可以按照以下方式进行操作。这将显示一个带有消息的警报对话框(如果您不需要该消息,只需删除该行)和图像(以及“确定”按钮):

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

You could do it in the following way. This will show an alertDialog with a message (if you don't need the message, just remove that line) and the image (and an OK button):

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();
八巷2024-11-21 23:08:36

还有另一个选项,您可以将图像放入 AlertDialog 中,而无需创建 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();

There is an another option you can put an image in AlertDialog without creating an xml file.

    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();
糖果控2024-11-21 23:08:36

对于图像我只是这样做:

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

For images I just do this:

public void onClick (View view) {
    new AlertDialog.Builder(this).setView(R.layout.your_layout).show();
}
终难愈2024-11-21 23:08:36

除了其他答案之外,我想补充一点,您不应该被迫使用 AlertDialog。例如,在我的例子中,我只想在对话框中显示 imageView,而 AlertDialog 不适用于这种情况。以下解决方案对我有用:

LayoutInflater factory = LayoutInflater.from(context);
final View view = factory.inflate(R.layout.myphoto_layout, null);
Dialog dialog = new Dialog(context);
dialog.setContentView(view);
dialog.show();

myphoto_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/someImage" />

如果您想动态地将图像添加到布局中,您可以使用以下代码:

ImageView iv = view.findViewById(R.id.iv); // id of your imageView element
iv.setImageBitmap(itmap);

In addition to the other answers, i want to add the point that you should not be obliged to use AlertDialog. For example, in my case i want to show only imageView in a dialog and AlertDialog didn't worked for this case. The below solution worked for me :

LayoutInflater factory = LayoutInflater.from(context);
final View view = factory.inflate(R.layout.myphoto_layout, null);
Dialog dialog = new Dialog(context);
dialog.setContentView(view);
dialog.show();

myphoto_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/someImage" />

If you want to add image to your layout dynamically, you can use the below code :

ImageView iv = view.findViewById(R.id.iv); // id of your imageView element
iv.setImageBitmap(itmap);
年华零落成诗2024-11-21 23:08:36

试试这个方法:

private void dialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(MainMenuActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View team = inflater.inflate(R.layout.image_layout, null);
    alert.setView(team);
    alert.show();
}

此外,实现一个名为 image_layout 的布局,并将所需的 UI 元素放入其中。

Try this method:

private void dialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(MainMenuActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View team = inflater.inflate(R.layout.image_layout, null);
    alert.setView(team);
    alert.show();
}

In addition, implement a layout named image_layout and put the desired UI elements into it.

大海や2024-11-21 23:08:36

在 kotlin 中,只需将 png 文件拖放到“res”文件夹中即可。就我而言,“res/drawable”。然后,使用以下代码:

    binding.myButton.setOnClickListener {
        val image = ImageView(requireContext()) 
        var myDialog: AlertDialog.Builder? = null
        image.setImageResource(R.drawable.my_png_file)
        myDialog = AlertDialog.Builder(
            requireContext(),
            R.style.AppCompatAlertDialogStyle
        )
        myDialog?.setTitle(getString(R.string.dialogTitle))
        myDialog?.setMessage(getString(R.string.dialogMessage))
        myDialog?.setPositiveButton(getString(R.string.closeDialog)) { dialog, _ ->
            dialog.dismiss()
        }
        myDialog?.setView(image)
        myDialog?.show()
}

png 文件应该已经包含边距,以便图像不会拉伸到对话框的边缘。我使用 Photoshop 缩小图像并创建边距(边距在下面以红色表示):

在此处输入图像描述

结果(注意左右边距)

在此处输入图像描述

In kotlin, just drag and drop a png file into "res" folder. In my case "res/drawable". Then, use this code:

    binding.myButton.setOnClickListener {
        val image = ImageView(requireContext()) 
        var myDialog: AlertDialog.Builder? = null
        image.setImageResource(R.drawable.my_png_file)
        myDialog = AlertDialog.Builder(
            requireContext(),
            R.style.AppCompatAlertDialogStyle
        )
        myDialog?.setTitle(getString(R.string.dialogTitle))
        myDialog?.setMessage(getString(R.string.dialogMessage))
        myDialog?.setPositiveButton(getString(R.string.closeDialog)) { dialog, _ ->
            dialog.dismiss()
        }
        myDialog?.setView(image)
        myDialog?.show()
}

The png file should already contain margins, so that the image doesn't stretch to the edges of the dialog. I used photoshop to shrink the image and create the margins (margins are represented in red below):

enter image description here

Result (notice the left and right margins)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文