【问题标题】:How to show appropriate icon on dialog box如何在对话框上显示适当的图标
【发布时间】:2011-10-05 07:35:46
【问题描述】:

我有一个允许用户删除视频文件的应用程序。当我按下删除按钮时,我正在使用

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:
            // mycode........
            break;
        case DialogInterface.BUTTON_NEGATIVE:
            // mycode.....
            break;
        }
    }
};

但此消息没有我们在 android 设备中看到的警告或删除图标。谁能帮助我获取这些图标或使用任何其他可以显示这些图标的警报对话框?

【问题讨论】:

  • 请发布代码,您正在创建 AlertDialog。
  • 先生,我用过对话框(不是警报对话框),想要一个使用警报对话框的例子。你能给我一个吗..

标签: android dialog icons alert


【解决方案1】:

我倾向于像官方文档示例中显示的那样使用 AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   })
   //Set your icon here
   .setTitle("Alert!")
   .setIcon(R.drawable.icon);
AlertDialog alert = builder.create();
 alert.show();//showing the dialog

至于实际的图标看看你的 sdk 文件夹/平台/android 版本#/data/res/drawable-mdpi 什么的

【讨论】:

  • 我尝试设置drawable但它没有显示在对话框中
  • 嗯,也试试设置标题。我想这可能是你没有看到的原因。试试我编辑的代码。
  • 非常感谢先生。它起作用了..你能告诉我更多关于其他功能的信息吗?比如把图像放在中心等。再次感谢
  • 为此,您必须使用自己的预定义视图创建自定义对话框。
  • @AlamKanak 您的编辑完全错误...setIcon() 已超载并接受Drawable 实例和可绘制资源ID!
【解决方案2】:

只需替换.setIcon(R.drawable.icon);.setIcon(getResources().getDrawable(R.drawable.icon)); 但它已被弃用。

【讨论】:

    【解决方案3】:

    为了设置默认对话框图标使用:

    .setIcon(android.R.drawable.ic_dialog_alert)
    

    还有更多可用的图标:

    • android.R.drawable.ic_dialog_dialer
    • android.R.drawable.ic_dialog_info
    • android.R.drawable.ic_dialog_email
    • android.R.drawable.ic_dialog_map

    【讨论】:

      【解决方案4】:

      另一种(肮脏的)方式:

      TypedValue typedValue = new TypedValue();
      getTheme().resolveAttribute(android.R.attr.alertDialogIcon, typedValue, true);
      
      new AlertDialog.Builder(this)
          .setIcon(typedValue.resourceId)
          ...
          .show();
      

      【讨论】:

        【解决方案5】:

        您还可以将警报对话框按钮的某些图标设置为:

        builder.setPositiveButtonIcon(getResources().getDrawable(drawable.ic_menu_share)

        【讨论】:

          【解决方案6】:

          只需为您设置 AlertDialog 的标题。像这样:

          adb.setTitle(R.string.app_name);
          

          完整示例:

          AlertDialog.Builder adb = new AlertDialog.Builder(this);
          adb.setIcon(R.mipmap.ic_launcher_foreground);
          adb.setTitle(R.string.app_name);
          adb.show();
          

          【讨论】:

            猜你喜欢
            • 2011-02-09
            • 1970-01-01
            • 2022-11-02
            • 2019-10-02
            • 2020-05-01
            • 2019-11-19
            • 2017-11-16
            • 1970-01-01
            • 2020-05-06
            相关资源
            最近更新 更多