Android:AlertDialog 在片段中看起来不同

Android: AlertDialog looks different in fragment

我有一个使用 TabLayout 和 Fragments 的应用程序,但是我的初始登录屏幕是标准的 Activity。当我从登录屏幕显示一个警告对话框时,该对话框的外观与我从片段内部显示一个时完全不同。

来自登录屏幕

来自片段内部

我用来显示 alertDialog 的代码如下 class

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.TextView;

class AlertDialogManager {

    private AlertDialog alertDialog;
    private Context mContext;

    public void showAlertDialog(final Activity activity, String title, String message, Boolean status, final Boolean finishOnClose) {
        // Set our context correctly based on what was passed in activity
        mContext = (activity.getParent()!=null) ? mContext = activity.getParent() : activity;

        // Create our alertDialog Builder
        alertDialog = new AlertDialog.Builder(mContext).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);        

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting alert dialog icon
        if(status != null) alertDialog.setIcon((status) ? R.drawable.icon_check : R.drawable.icon_alert);

        // Setting OK Button
        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // if the user passed in TRUE for the finishOnClose param - we call try onBackPressed first and if that fails, call finish()
                if (finishOnClose) {
                    try {
                        activity.onBackPressed();
                    } catch (Exception e) {
                        activity.finish();
                    }               
                }
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
}

要在 Activity 中显示一个,我使用以下内容:

// At the top of my activity I declare
private final AlertDialogManager alertDialog = new AlertDialogManager();

// Then where I want to show one I use this
alertDialog.showAlertDialog(MyActivity.this, "Title", "Message", false, false);

要在片段中显示一个,我使用以下内容:

// At the top of my fragment I declare
private final AlertDialogManager alertDialog = new AlertDialogManager();

alertDialog.showAlertDialog(getActivity(), "Title", "Message", false, false);

谁能解释为什么当我从 Activity vs Fragment 调用时,我的对话框中会出现两个完全不同的 "themes"?我被难住了。

谢谢!!!

您支持的 API 旧版本是什么?因为您可以使用自 API 11 以来的 AlertDialog builde。如果您支持旧版本,则必须设置主题。

示例:

    ContextThemeWrapper theme;
    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
        theme = new ContextThemeWrapper( context, android.R.style.Theme_Holo_Light_Dialog_NoActionBar );
    }
    else {
        theme= new ContextThemeWrapper( context, android.R.style.Theme_Light_NoTitleBar );
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(theme);

希望对您有所帮助。

结果我需要将以下内容添加到我的登录清单中的声明中 activity

android:theme="@style/Theme.AppCompat.NoActionBar"