如何设置对话框外边距?(安卓)

Ann*_*nna 3 android margins android-layout android-alertdialog

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

LMa*_*ker 7

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

您可以使用 ainset将边距设置为 a 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>
Run Code Online (Sandbox Code Playgroud)

看,该android:drawable属性正在调用我的对话框自定义背景。在我的情况下,是一个圆形的白色窗口。

这是我的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>
Run Code Online (Sandbox Code Playgroud)

现在你需要把它android:background="@drawable/dialog_isset"放在你parent view自定义对话框布局中


Pan*_*her -7

尝试这个

dialog.getWindow().getDecorView().setTop(100);
dialog.getWindow().getDecorView().setLeft(100);
Run Code Online (Sandbox Code Playgroud)