【问题标题】:How to add margins to Left and Right to Custom AlertDialog?如何在自定义 AlertDialog 的左右添加边距?
【发布时间】:2019-04-14 13:44:10
【问题描述】:

我已经创建了一个这样的自定义警报对话框 -

remark_builder = new AlertDialog.Builder(this);
        // Get the layout inflater
        LayoutInflater inflater = this.getLayoutInflater();
        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the
        // dialog layout
        remark_builder.setCancelable(true);
        final View dialogView = inflater.inflate(R.layout.add_remark_dialog, null);
        remark_builder.setView(dialogView);

if (remark_dialog == null || !remark_dialog.isShowing()) {

            remark_dialog = remark_builder.create();
            remark_dialog.setCanceledOnTouchOutside(false);
            remark_dialog.show();

        }

我想在此警报对话框的左侧和右侧添加边距。

我在互联网上尝试了很多代码,但它强调设置警报对话框的宽度和高度。 我不想设置宽度和高度。我想在 MATCH_PARENT 的 AlertDialog 的左侧和右侧添加边距。

我当前的警报对话框如下所示:

我想添加 40 或 50 等边距,或者根据设备密度添加边距。这可能吗?

【问题讨论】:

  • 你想让它变小吗?
  • 是的,宽度。您确定提供的链接有效吗??
  • 二进制 XML 文件第 2 行:膨胀类 出错。
  • 请提供对话框的xml布局

标签: android android-layout android-alertdialog


【解决方案1】:

您可以尝试以下方法来实现。

     Rect displayRectangle = new Rect();
            Window window = mContext.getWindow();

            window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
remark_dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        malertdialog.show();
        malertdialog.getWindow().setLayout((int)(displayRectangle.width() *
                0.8f), (int)(displayRectangle.height() / 2));

根据您需要的比例调整宽度和高度的十进制值。

【讨论】:

  • 您的代码正在运行,但存在一个问题。对话框不在中心。我尝试使用此设置重力 - remark_dialog.getWindow().setGravity(Gravity.CENTER);但它不工作。有什么建议吗??
  • 我的备注对话框布局是 wrap_content。如何将对话框设置为重心?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多