如何在android对话框中设置图像视图?

时间:2016-02-23 06:58:57

标签: android android-layout android-image android-dialog

我正在尝试在android对话框中设置图像视图。如果我从图库中选择一个图像,则会在所选图像的对话框中显示。我将尝试从厨房获取图像并传递到警报对话框。

4 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:4)

嘿,请关注link..

首先,您必须将自定义对话框布局设置为对话框,如下所示。

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

答案 2 :(得分:3)

要在警告对话框中设置图像,您需要创建一个像这样的自定义对话框

dialog.xml

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

然后在您的活动中显示您的自定义对话框

diff(y) / diff(x)

答案 3 :(得分:1)

enter image description here

  

我使用这种简单的方法来显示 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);