我有一個使用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