警告对话框下的白色背景

18

为什么我的警告对话框下面有白色背景?我已经试了一个小时都没有找到问题所在。请有人帮帮我吗?

另外,为什么标题的左右两侧有一点暗色阴影。

输入图片描述

protected void onPostExecute(String result) {
    //progressDialog.dismiss();
    try {
        JSONObject json = new JSONObject(result);
        String status = json.getString("status");
        String message = json.getString("message");
        if(status.equals("true")) {
            Intent intent = new Intent(context, HomeActivity.class);
            intent.putExtra(LoginActivity.EXTRA_MESSAGE, status);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
        else{
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setMessage(message)
                   .setTitle("Error")
                   .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                           dialog.cancel();
                       }
                   }).create().show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

使用自定义对话框视图。 - cyberlobe
你在哪个模拟器或设备上运行了你的应用程序? - user1922137
尝试像这样设置主题:AlertDialog.Builder(activity, android.R.style.Theme_Holo_Dialog); - Harin
@Harry,不起作用,我仍然得到白色背景。 - Drunken Daddy
@yaa110 我正在使用 Nexus 4 运行。 - Drunken Daddy
我找到了问题所在。我已经在样式中添加了<item name="android:background">#FFFFFF</item>。我删除了那一行,白色背景就消失了。很奇怪。 - Drunken Daddy
4个回答

29

使用 android.support.v7.app.AlertDialog 代替 android.app.AlertDialog


14

将您的代码更改为 -

 Dialog alertDialog = new Dialog(this);
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.setContentView(R.layout.tabs);
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    alertDialog.show();

或者您可以在现有的代码基础上添加主题。

AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

7
在初始化对话框构建器时,将第二个参数作为主题传递。因此,请更改


AlertDialog.Builder builder = new AlertDialog.Builder(activity);

为了

AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

这是一个旧的答案,现在应该使用 android.support.v7.app.AlertDialog 代替 android.app.AlertDialog

请参考被接受的答案


1
它可以工作,但现在标题和消息之间以及确定按钮之间的分隔符也消失了。 :) - Drunken Daddy
请参考以下链接以解决此问题:https://dev59.com/_V8d5IYBdhLWcg3woDYZ - Giru Bhai

6

如果您可以访问Dialog类,请尝试以下操作:

 alertDialog.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);    

之前:

dialog_before

之后:

dialog_after


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接