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

lbl*_*asa 52 android dialog

有谁知道我如何设置自定义对话框的边距?我问,因为我有一个自定义对话框但是在显示时它会延伸以填充父对象,即使我在布局参数上明确设置了WRAP_CONTENT.

基本上,对话框包含一个listview,其元素必须向下滚动,当元素为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>
Run Code Online (Sandbox Code Playgroud)

pos*_*spi 97

边距不适用于Dialogs,我认为顶级窗口视图不是支持边距的布局类型.我已经看过帖子,当定义为Dialog的样式(而不是顶级视图元素)时,边距将起作用,但这似乎也不起作用.

解决这个问题需要做的是inset为对话框背景使用drawable,并调整任何填充以考虑背景的额外插入.在下面的示例中,我将设置左右边距.

对话背景drawable:

<?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>
Run Code Online (Sandbox Code Playgroud)

对话主视图:

<?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... -->
Run Code Online (Sandbox Code Playgroud)

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

<color name="transparent">#00000000</color>
Run Code Online (Sandbox Code Playgroud)

并将对话框的窗口背景颜色设置为此(注意:你不能直接指定颜色,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>
Run Code Online (Sandbox Code Playgroud)

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

  • @ChrisJD我无法让它运转起来.无论如何,pospi解决方案正在发挥作用.有时真的很奇怪Android需要多少奇怪的代码才能实现非常简单的事情. (3认同)
  • 如果在对话框样式中使用dialog_background_inset drawable作为windowBackground,并且不使用对话框布局的背景,则不必担心在布局中包含额外的填充以允许插入. (2认同)
  • @pospi您知道如何在对话框内容之外单击时使其可取消吗?因为使用您的解决方案不起作用。 (2认同)

小智 32

我遇到了同样的问题,我通过在InsetDrawable上设置窗口背景来解决它:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
...
...
AlertDialog dialog = builder.create();

ColorDrawable back = new ColorDrawable(Color.TRANSPARENT);
InsetDrawable inset = new InsetDrawable(back, 20);
dialog.getWindow().setBackgroundDrawable(inset);

dialog.show();
Run Code Online (Sandbox Code Playgroud)

在这种情况下,对话框将显示所有边缘的边距为20.

  • 这也适用于“DialogFragment”,甚至负值(原文如此!):“new InsetDrawable(back, 16, -8, 16, -8);”。伟大的! (2认同)

Nim*_*van 17

边距似乎不适用于对话框的自定义布局,但填充有效.尝试在顶层线性布局上设置填充.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="4dp"
    android:paddingRight="4dp" >
Run Code Online (Sandbox Code Playgroud)


Jes*_*era 11

正如@simpleApps 和@pospi 指出的那样,您需要向Dialog 的backgroundDrawable 添加插图。如果您使用的是自定义对话框,则可以尝试使用 xml 中定义的 drawable

dialog?.window?.setBackgroundDrawableResource(R.drawable.background_dialog)
Run Code Online (Sandbox Code Playgroud)

其中 R.drawable.background_dialog 定义为

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="20dp"
    android:insetTop="40dp"
    android:insetRight="20dp"
    android:insetBottom="40dp">
    <shape android:shape="rectangle">
        <solid android:color="@color/color_background_fragment" />
        <corners android:radius="10dp" />
    </shape>
</inset>
Run Code Online (Sandbox Code Playgroud)


The*_*Rex 8

我在使用其他解决方案时遇到了麻烦,因为“确定”和“取消”按钮超出了屏幕,看起来很奇怪。相反,我使用了android:windowMinWidthMinor 和 android:windowMinWidthMajor,它们看起来更好,而且需要的代码也更少。Java/Kotlin 中不需要任何东西。

这就是我所做的一切:

1)仅使用一个背景可绘制对象(我称之为dialog_box.xml)

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorWhite" />
    <corners android:radius="18dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)

2)在Layout中设置背景:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/dialog_box">

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

3)设置样式:

    <item name="alertDialogTheme">@style/AlertDialogTheme</item>

    <style name="AlertDialogTheme" parent="ThemeOverlay.AppCompat.Dialog.Alert">
        <item name="android:background">@drawable/dialog_box</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowMinWidthMinor">85%</item>
    </style>
Run Code Online (Sandbox Code Playgroud)


Moh*_*abi 7

解决方法可以这样做:

dialog.getWindow().getAttributes().height = 
           (int) (getDeviceMetrics(context).heightPixels*0.8);
Run Code Online (Sandbox Code Playgroud)

` getDeviceMetrics方法:

public static DisplayMetrics getDeviceMetrics(Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getMetrics(metrics);
    return metrics;
}
Run Code Online (Sandbox Code Playgroud)