如何在Alert对话框中设置ImageView

How to set ImageView in the AlterDialog Box

ImageView imageView = new ImageView(context);
        imageView.setImageResource(R.mipmap.ic_launcher);

        AlertDialog dialog = new AlertDialog.Builder(context)
                .setView(imageView)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();

        dialog.show();
        AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.CustomDialogTheme);
        builder.setTitle("Explanation");
        builder.setMessage(list.get(position).getExplaination());
        url = list.get(position).getImageUrl();
        Log.i("URL", url);

        builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                // dismiss dialog
                dialogInterface.dismiss();
            }
        });
        builder.show();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomDialog">

    <ImageView
        android:id="@+id/eImageView"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="10dp"/>

    <TextView
        android:id="@+id/eTextView"
        android:layout_width="150dp"
        android:layout_height="300dp"
        android:layout_margin="8dp"
        android:gravity="center"
        android:padding="20dp"
        android:text=""
        android:textColor="#000000"
        android:translationX="120dp"
        android:translationY="10dp"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

您不能将 URL 直接设置到 Imageview 中,因为您必须使用这些库

Glide

Picasso

滑行示例:

Glide.with(context).load(your_url).into(imageview);

Picass 示例:

Picasso.get().load(your_url).into(imageview);

您可以使用 Dialog class。我正在写下面的代码

Dialog dialog = new Dialog(context);
dialog.setCancelable(true);
ImageView imageView = dialog.findViewById(imageView); 
TextView textView = dialog.findViewById(textView); 
dialog.show();
dialog.getWindow().setBackgroundDrawable(new 
ColorDrawable(context.getResources().getColor(android.R.color.transparent)));
                   

要设置需要从 CustomDialog XML 文件中获取 ImageView id 的图像,然后可以将特定图像设置到 ImageView 中。

因此,首先,使用 getLayoutInflater() 获取您的自定义视图。

注意:根据您的要求使用以下选项之一。

View view = getLayoutInflater().inflate(R.layout.CustomDialog, null); // for activity
View view = ((ViewHolder) holder).mainActivity.getLayoutInflater().inflate(R.layout.CustomDialog, null); // for adapter class
View view = getActivity().getLayoutInflater().inflate(R.layout.CustomDialog, null); // for fragment

然后,将view添加到builder.setView();

builder.setView(view);

但是,您还需要获取位于 CustomDialog XML 文件中的所有视图的 ID。

TextView textview = view.findViewById(R.id.eTextView);
ImageView imageview = view.findViewById(R.id.eImageView);

现在,您可以使用 Glide 依赖项将图像设置到 ImageView

Glide.with(context).load(url).into(imageview);

完整示例:

    View view = getLayoutInflater().inflate(R.layout.CustomDialog, null);
    ImageView imageview = view.findViewById(R.id.eImageView);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Explanation");
    builder.setView(view);
    builder.setMessage(list.get(position).getExplaination());
    url = list.get(position).getImageUrl();
    Glide.with(context).load(url).into(imageview);
    Log.i("URL", url);

    builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // dismiss dialog
            dialogInterface.dismiss();
        }
    });
    builder.show();

I have also created a CustomDialogBox view but am not able to understand how should I pass the text and imageUrl value to that particular AlertDialogBox.

当您创建自定义对话框时 class 设置带有文本和图像参数的承包商 url。在您的视图中使用此文本和 imgUrl。使用 Picasso 或 Glide 从在线加载图像 URL.

/*** On Activity class when you show your custom Dialog Box ****/

//Pass the text and img URL to your Custome Dialog constructor while create an instance
    DialogFragment dialogBox= new CustomDialogBox(text, ImgUrl); 
    dialogBox.show(getSupportFragmentManager(), "custom_dialog_tag");

 // Custom DialogBox Class
 public class CustomDialogBox extends DialogFragment{
    private String text;
    private String imgUrl;
            
    //Constructor to receive argument from Activity    
    public CustomDialogBox (String text, String imgUrl) {
       this.text = text;
       this.imgUrl= imgUrl;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
                
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      // Get the layout inflater
      View view = getLayoutInflater().inflate(R.layout.custom_dialog, null); // for activity
    
       // Inflate and set the layout for the dialog
       builder.setView(view)
       TextView textView = view.findViewById(R.id.yourTextView);
       ImageView imgView= view.findViewById(R.id.yourImgView);

       // View online img with glide library
        Glide.with(context).load(imgUrl).into(imgView); 

        return builder.create();
       }
    }

要了解有关自定义对话框的更多信息 - Official Documentation