有人知道如何为自定义对话框设置边距吗?我之所以问是因为我有一个自定义对话框,但是当显示它时它会拉伸以填充父级,即使我在布局参数上明确设置了 WRAP_CONTENT 。
基本上,对话框包含一个列表视图,其元素必须向下滚动,例如当元素为 1 时,它不会拉伸,但是当添加更多项目时,对话框会占据整个屏幕。
有什么建议么?我已经尝试了所有可能的解决方案组合,但没有取得令人满意的结果
编辑:添加了对话框布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="50dip"
android:orientation="vertical"
android:layout_gravity="top|center">
<FrameLayout android:layout_width="fill_parent" android:layout_margin="5dip" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp" android:textColor="@color/black"/>
<Button android:layout_height="32dip" android:layout_width="32dip"
android:id="@+id/guide_dialog_cross_button"
android:background="@drawable/button_cross_white"/>
</FrameLayout>
<ListView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fadingEdge="none"
android:layout_margin="5dip"/>
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_margin="5dip" />
</LinearLayout>
原文由 lblasa 发布,翻译遵循 CC BY-SA 4.0 许可协议
边距不适用于对话框,我想顶级窗口视图不是支持边距的布局类型。我看过帖子说边距在定义为对话框的样式时会起作用(而不是在顶级视图元素上),但这似乎也不起作用。
要解决此问题,您需要做的是使用
inset
为您的 Dialog 背景绘制,并调整任何填充以考虑背景的额外插图。在下面的示例中,我将只设置左右边距。对话框背景可绘制:
对话框主视图:
您可能还需要将 Dialog 本身的背景颜色设置为透明。像这样添加颜色资源:
并将对话框的窗口背景颜色设置为此(注意:不能直接指定颜色,eclipse会报错)
此样式应作为
theme
参数传递给对话框的构造函数,如new Dialog(context, R.style.Dialog);