我可以创建和显示一个自定义警报对话框很好,但即使这样,我有android:layout_width/height=“fill_parent”在对话xml中,它只和内容一样大。

我想要的是填充整个屏幕的对话框,除了20像素的填充。 然后,作为对话框一部分的图像将自动使用fill_parent拉伸到完整的对话框大小。


当前回答

这里其他的答案都说得通,但它不符合费边的要求。这是我的一个解决办法。这可能不是完美的解决方案,但对我来说很管用。它显示了一个全屏的对话框,但你可以在顶部、底部、左边或右边指定一个填充。

首先把它放在res/values/styles.xml中:

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/Black0Percent</item>
    <item name="android:paddingTop">20dp</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowIsFloating">false</item>
</style>

正如你所看到的,我有android:paddingTop= 20dp基本上是你需要的。android:windowBackground = @color/Black0Percent只是在color.xml中声明的颜色代码

res /价值/ color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Black0Percent">#00000000</color>
</resources>

该颜色代码只是作为一个虚拟,以取代对话框的默认窗口背景与0%透明度的颜色。

接下来构建自定义对话框布局res/layout/dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialoglayout"
    android:layout_width="match_parent"
    android:background="@drawable/DesiredImageBackground"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edittext1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="18dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dummy Button"
        android:textSize="18dp" />

</LinearLayout>

最后这是我们的对话框,它设置了使用dialog.xml的自定义视图:

Dialog customDialog;
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View customView = inflater.inflate(R.layout.dialog, null);
// Build the dialog
customDialog = new Dialog(this, R.style.CustomDialog);
customDialog.setContentView(customView);
customDialog.show();

结论:我试图在名为CustomDialog的styles.xml中覆盖对话框的主题。它覆盖了对话框窗口布局,让我有机会设置填充和改变背景的不透明度。这可能不是完美的解决方案,但我希望它能帮助你…:)

其他回答

您需要使用样式@style.xml,例如CustomDialog来显示可自定义的对话框。

<style name="CustomDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/colorWhite</item>
    <item name="android:editTextColor">@color/colorBlack</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

在activity。java中使用这种样式

Dialog dialog = new Dialog(Activity.this, R.style.CustomDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);

你的custom_dialog.xml应该在你的布局目录中

<?xml version="1.0" encoding="utf-8"?>
<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="10dp"
    android:paddingRight="10dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="20dp"
        android:id="@+id/tittle_text_view"
        android:textColor="@color/colorBlack"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="10dp"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="20dp">

        <EditText
            android:id="@+id/edit_text_first"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

        <EditText
            android:id="@+id/edit_text_second"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:hint="0"
            android:layout_marginLeft="5dp"
            android:inputType="number" />

        <TextView
            android:id="@+id/text_view_second"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:gravity="center"/>

    </LinearLayout>

</LinearLayout>

只要给AlertDialog这个主题

<style name="DialogTheme" parent="Theme.MaterialComponents.Light.Dialog.MinWidth">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

您可以使用百分比(JUST)窗口对话框宽度。

看看这个来自Holo Theme的例子:

<style name="Theme.Holo.Dialog.NoActionBar.MinWidth">
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
</style>

 <!-- The platform's desired minimum size for a dialog's width when it
     is along the major axis (that is the screen is landscape).  This may
     be either a fraction or a dimension. -->
<item type="dimen" name="dialog_min_width_major">65%</item>

你所需要做的就是扩展这个主题,并将“Major”和“Minor”的值改为90%而不是65%。

的问候。

如果你正在使用约束布局,你可以在其中设置任何视图,以填充屏幕的百分比:

layout_constraintWidth_percent = " 0.8 "

例如,如果你在对话框中有一个ScrollView你想把它设置为屏幕高度的一个百分比。它是这样的:

<ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0.8">

希望它能帮助到一些人!!

dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);