【问题标题】:Android - Icon not shown in AlertDialogAndroid - 图标未显示在 AlertDialog 中
【发布时间】:2015-02-23 08:51:06
【问题描述】:

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

我可以运行应用程序,对话框显示没有图标,没有错误。
但是 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();

【问题讨论】:

  • .setIconAttribute(android.R.attr.alertDialogIcon)

标签: android


【解决方案1】:

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

设置为空字符串:

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

【讨论】:

    【解决方案2】:

    你应该使用这样的东西:

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

    试试这个:)

    【讨论】:

    • 这给了我“无法解析方法 getResources()”
    • getDrawable() 在 API 22 中已弃用
    【解决方案3】:

    您只需要添加标题,因为您的图标的位置在您的标题旁边。

    .setTitle("Attention")
    .setIcon(android.R.drawable.ic_dialog_alert)  
    

    【讨论】:

      【解决方案4】:

      您可能还需要设置标题。如果你不喜欢新的布局,你可能想看看这个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】:

        您可以使用dialogBuiler.setView(your_custom_layout)

        例如 custom_dialog_layout.xml 你可以根据自己的喜好来美化

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="7dp"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:id="@+id/custom_diag_img_view"
                    android:src="@android:drawable/ic_dialog_alert"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/custom_diag_title_tv"
                    style="@style/TextAppearance.AppCompat.Headline"/>
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/custom_diag_msg_tv"
                android:layout_marginTop="15dp"
                style="@style/TextAppearance.AppCompat.Subhead"/>
        
        </LinearLayout>
        

        Utils.java

        public static void showAlertDialog(Context activityContext, Integer iconResource, String title, String message){
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activityContext, R.style.Theme_MaterialComponents_DayNight_Dialog_Alert);
            LinearLayout customLL = (LinearLayout) LayoutInflater.from(activityContext).inflate(R.layout.custom_dialog_layout, null);
            ImageView icon = customLL.findViewById(R.id.custom_diag_img_view);
            if(iconResource != null)
                icon.setImageResource(iconResource);
            android.widget.TextView titleTV = customLL.findViewById(R.id.custom_diag_title_tv);
            titleTV.setText(title);
            TextView msgTV= customLL.findViewById(R.id.custom_diag_msg_tv);
            msgTV.setText(message);
            dialogBuilder
                    .setView(customLL)
                    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //some code
                        }
                    })
                    .show();
        }
        

        MyActivity.java

            //without title
        Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, null, getString(R.string.allowed_images));
        
        //with title
        Utils.showAlertDialog(UploadActivity.this,R.drawable.ic_error, "Some title, getString(R.string.allowed_images));
        

        您可以添加复选框或其他操作

        【讨论】:

          猜你喜欢
          • 2021-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-15
          • 2018-03-02
          • 2020-08-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多