2015-02-23 70 views
2

我有一个警告对话框,它不显示我使用“setIcon”设置的图标。
我使用Android Studio和Android Emulator API 19/Android 4.4.2。Android - AlertDialog中未显示图标

我可以运行该应用程序,对话框显示没有图标,没有错误。
但Android Studio标记包含“setIcon”的行,并提供给我“引入本地变量”和“使方法调用链进入调用序列”

所以我的问题:为什么图标不显示?

我的代码:

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

builder 

.setMessage("Repeat game?") 
.setIcon(android.R.drawable.ic_dialog_alert)   //<--- icon does not show 
.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     //Yes button clicked, do something 

    } 
}) 
.setNegativeButton("No", null)      //Do nothing on no 
.show(); 
+0

.setIconAttribute(android.R.attr.alertDialogIcon) – 2017-10-05 05:09:28

回答

4

你应该使用这样的事情:

builder.setIcon(getResources().getDrawable(android.R.drawable.ic_dialog_alert)); 

试试这个:)

+0

这给了我 “无法解析方法getResources()” – Spacewalker 2015-02-23 10:03:28

+0

context.getResources(..).. – 2015-02-23 10:04:37

0

这可能是你需要设置标题为好。如果你不喜欢新的布局,你可能想看看这个question

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder 
    .setMessage("Repeat game?") 
    .setTitle("") 
    .setIcon(android.R.drawable.ic_dialog_alert)   //<--- icon does not show 
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      //Yes button clicked, do something 
     } 
    }) 
    .setNegativeButton("No", null)      //Do nothing on no 
    .show(); 
5

您的标题是空的,所以android不会显示它,因此也不会显示任何图标。

它设置为空字符串:

builder.setTitle("")...<all the other stuff>