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

问题描述 投票:2回答:4

我试图在Android对话框中显示一张图片。如果我从图库应用中选择一张图片,它应该在对话框中显示。我试着从图库应用中获取所选图片,并将其路径传递给警报对话框。

android android-layout android-alertdialog android-image android-dialog
4个回答
7
投票
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();

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

3
投票

为了在警报对话框中设置图像,你需要创建一个自定义对话框,像这样。

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:

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);
© www.soinside.com 2019 - 2024. All rights reserved.