简体   繁体   中英

Android alert dialog - image on top

Hey I'm trying to put an image above the title in the alert dialog. This is what I'm doing:

ImageView image = new ImageView(this);
image.setImageResource(R.drawable.img);

alertDialog = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault_Light_Dialog))
        .setView(image)
        .setCancelable(false)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton(btn1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {}})
        .setNegativeButton(btn2, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {}})
        .create();
alertDialog.show();

This is the result : 在此处输入图片说明

And This is what I'm trying to achieve: 在此处输入图片说明

I've also tried it with setIcon but then the image appears on the left of the title and not above.

Simply use this solution

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.some_icon);
dialog.show();

You have to create custom layout in which you can design which you want. Becuase you can not more customization in default existing alert dialog.

Custom Alert dialog you can create with ref url : http://javatechig.com/android/android-custom-dialog-example

You have to create custom dialog and inflate that layout for CustomDialog. You can view Custome dialog here.

Create Custom dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM