【问题标题】:How to set dialog outer margins? (Android)如何设置对话框外边距? (安卓)
【发布时间】:2014-10-13 09:02:48
【问题描述】:

我需要在对话框周围添加自定义边距。任何建议如何做到这一点? (对话框位置顶部:100 px,左侧 50 px 等)

【问题讨论】:

  • 通过设置layout_margin 我想,不是吗?
  • 不要使用 px 使用 dp 代替 ....
  • 你是使用自定义对话框还是默认简单的对话框

标签: android android-layout android-alertdialog margins


【解决方案1】:

我知道这是一个老问题,但如果还有人需要帮助...

您可以使用inset 将边距设置为Custom dialog

在您的可绘制文件夹中创建一个dialog_inset.xml

<inset
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/dialog_background"
android:insetRight="15dp"
android:insetBottom="15dp"
android:insetLeft="15dp">
</inset>

看,android:drawableattribute 正在调用我的对话框自定义背景。就我而言,是一个圆形的白色窗口。

这是我的dialog_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
    <shape android:shape="rectangle">
        <corners android:radius="20dp"/>
        <solid android:color="#ffffff"/>
        <stroke android:color="#4c252525" android:width="1dp"/>
    </shape>
</item>
</selector>

现在您需要将此android:background="@drawable/dialog_isset" 放在您的自定义对话框布局parent view中。

【讨论】:

  • 很高兴能帮到你。
【解决方案2】:

我是这样做的:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.cutomDialog);
...
...
Dialog dialog = dialogBuilder.create();

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();

params.gravity = Gravity.TOP;
params.x = locationByX;
params.y = locationByY;
params.width = dialogWidth;
params.height = dialogHeight;

dialog.getWindow().setAttributes(params);

【讨论】:

    【解决方案3】:

    试试这个

    dialog.getWindow().getDecorView().setTop(100);
    dialog.getWindow().getDecorView().setLeft(100);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-03
      • 2023-03-23
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2012-01-10
      相关资源
      最近更新 更多