如何为自定义对话框设置边距?

新手上路,请多包涵

有人知道如何为自定义对话框设置边距吗?我之所以问是因为我有一个自定义对话框,但是当显示它时它会拉伸以填充父级,即使我在布局参数上明确设置了 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 许可协议

阅读 1k
2 个回答

边距不适用于对话框,我想顶级窗口视图不是支持边距的布局类型。我看过帖子说边距在定义为对话框的样式时会起作用(而不是在顶级视图元素上),但这似乎也不起作用。

要解决此问题,您需要做的是使用 inset 为您的 Dialog 背景绘制,并调整任何填充以考虑背景的额外插图。在下面的示例中,我将只设置左右边距。

对话框背景可绘制:

 <?xml version="1.0" encoding="utf-8"?>
<!-- drawable is a reference to your 'real' dialog background -->
<!-- insetRight and insetLeft add the margins -->
<inset
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/dialog_background"
    android:insetRight="10dp"
    android:insetLeft="10dp">
</inset>

对话框主视图:

 <?xml version="1.0" encoding="utf-8"?>
<!-- paddingTop / paddingBottom padding for the dialog -->
<!-- paddingLeft / paddingRight padding must add the additional inset space to be consistent -->
<!-- background references the inset background drawable -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:background="@drawable/dialog_background_inset">

<!-- ...the rest of the layout... -->

您可能还需要将 Dialog 本身的背景颜色设置为透明。像这样添加颜色资源:

 <color name="transparent">#00000000</color>

并将对话框的窗口背景颜色设置为此(注意:不能直接指定颜色,eclipse会报错)

 <style name="Dialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
</style>

此样式应作为 theme 参数传递给对话框的构造函数,如 new Dialog(context, R.style.Dialog);

原文由 pospi 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以通过从 width 中删除 margin 来使您的 AlertDialog 与父母保持距离。 (因为 AlertDialog 放在中间,与父母等分。)

 Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alertdialog);
dialog.setCancelable(false);

  int device_with = Resources.getSystem().getDisplayMetrics().widthPixels;
  int margin = 40; // dp
  int width = (device_with - DPtoPX(margin));
  int height = ViewPager.LayoutParams.WRAP_CONTENT;
  dialog.getWindow().setLayout(width, height);

dialog.show();

这是 DPtoPX (Dip to Pixel)转换器方法:

 private int DPtoPX(int dp){
   return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

祝你好运。

原文由 Khoshghalb2c 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题