我需要在对话框周围添加自定义边距。您有什么建议吗? (对话框位置:上方100像素,左侧50像素等)
我需要在对话框周围添加自定义边距。您有什么建议吗? (对话框位置:上方100像素,左侧50像素等)
我知道这是一个老问题,但如果还有人需要帮助...
您可以使用 inset
来设置 自定义对话框
的边距。
在您的 drawable 文件夹中创建一个名为 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: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>
现在你需要将android:background="@drawable/dialog_isset"
添加到你的自定义对话框布局的父视图
中。
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);
试试这个
dialog.getWindow().getDecorView().setTop(100);
dialog.getWindow().getDecorView().setLeft(100);
layout_margin
,我想是吧? - Leo