我有一个使用TabLayout和Fragments的应用程序,但是我的初始登录屏幕是标准的Activity。当我从“登录”屏幕显示警告对话框时,对话框的外观与我从片段内部显示一个对话框的外观完全不同。
从登录屏幕
从片段内部
我用来显示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);
任何人都可以解释为什么在从Activity vs Fragment调用时我会在对话框中获得2个完全不同的“主题”?我很难过。
谢谢!!!
答案 0 :(得分:2)
您支持的旧API版本是什么?因为您可以使用自API 11以来的AlertDialog构建。如果您支持旧版本,则必须设置主题。
示例:
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);
希望得到这个帮助。
答案 1 :(得分:0)
原来我需要在我的登录活动
的清单中添加以下内容android:theme="@style/Theme.AppCompat.NoActionBar"