【问题标题】:How to customise the alertdialog in Android? [duplicate]如何在 Android 中自定义警报对话框? [复制]
【发布时间】:2021-05-31 15:30:03
【问题描述】:

我的应用程序中有一个AlertDialog,我希望为整个dialog 设置一个背景,并为两个按钮设置一个背景和文本颜色。我似乎无法自定义 alertDialog。我该怎么做?我希望对话框如下图所示。

【问题讨论】:

  • 查看MaterialAlertDialogBuilder
  • @BenP。谢谢,我检查了,这解决了我的问题!
  • @ADM 我按照上面 Ben 的建议使用了 MaterialAlertDialogBuilder

标签: android dialog android-alertdialog


【解决方案1】:

嗯,基本步骤是:

  • 创建您的自定义布局
  • 创建对话框构建器并设置自定义视图。 (builder.setView(your_custom_view))

那里有大量的教程,即使在 SOF 中也是如此。请先搜索一下。

https://suragch.medium.com/creating-a-custom-alertdialog-bae919d2efa5 https://www.journaldev.com/22153/android-custom-alert-dialog

【讨论】:

    【解决方案2】:

    首先,为对话框创建你的layout.xml,然后为示例代码调用你的对话框就像

    AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialog);
    View view = LayoutInflater.from(context).inflate(R.layout.yourlayout, null);
    builder.setView(view);
    final androidx.appcompat.app.AlertDialog alertDialog = builder.create();
    if (alertDialog.getWindow() != null) {
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    }
    alertDialog.setCancelable(true);
    alertDialog.show();
    

    如果你想在 layout.xml

    中声明你的视图
    TextView txexample = view.findViewById(R.id.txexample);
    

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 1970-01-01
      • 2021-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多