简体   繁体   中英

AlertDialog looks differently on Nexus devices

I'm creating different AlertDialogs in different scenarios in my application using the following code:

public static void showAlertDialog(Context activityContext, DialogType type, CharSequence title, CharSequence msg, CharSequence posText,
        DialogInterface.OnClickListener posOnClickListener, CharSequence negText, DialogInterface.OnClickListener negOnClickListener, boolean isCancelable, int iconResId) {
    try {
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activityContext);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) title))
            alertDialogBuilder.setTitle(title);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) msg))
            alertDialogBuilder.setMessage(msg);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) posText))
            alertDialogBuilder.setPositiveButton(posText, posOnClickListener);
        if (JavaUtils.isNotNullNotEmptyNotWhiteSpaceOnly((String) negText))
            alertDialogBuilder.setNegativeButton(negText, negOnClickListener);

        alertDialogBuilder.setCancelable(isCancelable);

        // set alert icon
        if (iconResId == 0) {
            alertDialogBuilder.setIcon(type == DialogType.ERROR ? android.R.drawable.ic_dialog_alert : android.R.drawable.ic_dialog_info);
        } else {
            alertDialogBuilder.setIcon(iconResId);
        }

        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialog.show();
    } catch (Exception e) {
        InfiLogger.w("showAlertDialog", e.toString(), e);
        Crashlytics.logException(e);
    }
}

While on most devices that run Lollipop or higher the AlertDialog looks like this:

在此处输入图片说明

On Nexus devices the same running code looks like this:

在此处输入图片说明

Does someone knows why this happens? And what is the proper way to fix this?

The solution for this problem is using the AlertDialog from the support library instead of using the original one.

So all I had to do to solve this issue was to replace this import:

import android.app.AlertDialog;

with this import:

import android.support.v7.app.AlertDialog;

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