警报对话框图标无法设置

时间:2011-09-11 21:54:46

标签: java android

我的drawable-hdpi文件夹中有一个14x14的png文件(icon_alert.png)。这就是我设置图标的方式:

alertDialog.setIcon(R.drawable.icon_alert);

显示对话框时,图标不存在。

3 个答案:

答案 0 :(得分:2)

你也需要一个标题,否则它将无效。

答案 1 :(得分:1)

public class ExampleApp extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setMessage("Do you want to close this window ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//  Action for 'NO' Button
dialog.cancel();
}
});
AlertDialog alert = alt_bld.create();
// Title for AlertDialog
alert.setTitle("Title");
// Icon for AlertDialog
alert.setIcon(R.drawable.icon);
alert.show();
}
}

答案 2 :(得分:1)

您可以使用alertDialog.setIcon(R.drawable.icon);

图标必须为.png.jpg,我强烈推荐16X16。