如何在Android中设置对话框图标

如何在Android中设置对话框图标,android,Android,我想在Android中自定义一个对话框。 我知道如何设置对话框的标题: dialog.setTitle("O message"); 现在我想在标题前面设置图标。 我怎样才能做到这一点 Dialog dialog; dialog = new Dialog(this); dialog.setContentView(R.layout.layouterror22); dialog.setTitle("O message"); 或 希望这有帮助。使用这个 dialog.setIcon(R.

我想在Android中自定义一个对话框。 我知道如何设置对话框的标题:

dialog.setTitle("O message");
现在我想在标题前面设置图标。 我怎样才能做到这一点

Dialog dialog;
dialog = new Dialog(this);
dialog.setContentView(R.layout.layouterror22);      
dialog.setTitle("O message");

希望这有帮助。

使用这个

dialog.setIcon(R.drawable.ic_launcher)

您需要更多的定制方式,请参阅本网站。您可能需要使用AlertDialog。如果你这样做了,就

AlertDialog.Builder b = new AlertDialog.Builder(yourContext);
b.setIcon(yourIcon);
/* add other properties thanks to b.set... */
b.create().show();

希望这对您有所帮助。

您可以添加带有以下代码的图标:

Dialog dialog = new Dialog(context);

dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon); 
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog Title");

dialog.show();

请参阅。

如果使用带片段的
viewPager
,则可以从
main活动的
onBackPressed()
调用
safeExit()
。这就是我所做的,我从来没有遇到过任何问题:

    @Override
    public void onBackPressed() {

        try {
            if (getFragmentManager().getBackStackEntryCount() > 1) {
                FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0);
                getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else
                safeExit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void safeExit() {
        new Builder(this).setIcon(R.drawable.ic_launcher).setTitle("Exit!").setMessage(
                "Close Application?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        }).setNegativeButton("No", null).show();
   }

要将图标添加到警报对话框,您需要向其添加以下代码

AlertDialog.Builder builder = new AlertDialog.Builder(this);
      /*  builder.setMessage("Visit Be Developers.tech");
        builder.setTitle("Alert");
    builder.setIcon(R.drawable.ic_baseline_call_24);//This line should be added

        builder.setPositiveButton("Visit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();
有关更多自定义(如添加列表),请参见

i get dialog.setIcon但不支持(注意:此拨号自定义)这可能是代码:dialog dialog;dialog=新建对话框(此对话框);setContentView(R.layout.layouterror22);对话框。设置标题(“O消息”)
AlertDialog
似乎需要将
setTitle()
应用于
setIcon()
才能工作。@jdv太棒了。我刚刚编写了我的setIcon(android.R.drawable.ic_dialog_alert)并想知道它为什么不工作,我在屏幕的另一边看了看,你的评论解释了原因。我得到dialog.setIcon但不支持(注:此拨号自定义)这是may code:dialog dialog;dialog=新建对话框(此对话框);setContentView(R.layout.layouterror22);对话框。设置标题(“O消息”);问题是关于对话,而不是警报对话。以上答案不适用于Dialog。见上面卡尔·博世的评论。这是一个对话,请不要AlertDialog@user1497597:参考此链接,它有一个示例,您可以更改行的顺序:这非常重要,因为在将任何内容推送到对话框之前必须请求功能。行的顺序:这非常重要!!!setFeatureDrawableResource(Window.FEATURE\u LEFT\u图标,R.drawable.your\u图标);应在setContentView之后调用
    @Override
    public void onBackPressed() {

        try {
            if (getFragmentManager().getBackStackEntryCount() > 1) {
                FragmentManager.BackStackEntry first = getFragmentManager().getBackStackEntryAt(0);
                getFragmentManager().popBackStack(first.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else
                safeExit();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void safeExit() {
        new Builder(this).setIcon(R.drawable.ic_launcher).setTitle("Exit!").setMessage(
                "Close Application?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        }).setNegativeButton("No", null).show();
   }
AlertDialog.Builder builder = new AlertDialog.Builder(this);
      /*  builder.setMessage("Visit Be Developers.tech");
        builder.setTitle("Alert");
    builder.setIcon(R.drawable.ic_baseline_call_24);//This line should be added

        builder.setPositiveButton("Visit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();