我有一个使用TabLayout和Fragments的应用程序,但是我的初始登录屏幕是标准的Activity。当我从登录屏幕显示警告对话框时,对话框的外观与从片段内部显示一个完全不同。Android:AlertDialog在片段中看起来不同
从登录屏幕
从内片段的
,我用它来显示alertDialog的代码下面的类
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();
}
}
要我用下面的活动表明一个:
// 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);
任何人都可以解释为什么我会完全得到2从Activity和Fragment进行调用时,我的对话框中有不同的“主题”?我很难过。
谢谢!!!
两个不同的Android版本? – Blackbelt
@Blackbelt - 我该如何检查?我有compileSdkVersion 23,minSdkVersion 19和targetSdkVersion 23 - 我的build.gradle – Phil
我的意思是在你运行应用程序的设备上。您使用的是哪个版本的AlertDialog? 'v7.app.AlertDialog'?检查您的导入 – Blackbelt