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

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


当前回答

dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);

其他回答

让你的对话成为一种活动。3个步骤

步骤1: 将其中一个放在styles.xml中

风格: 我喜欢这个,因为你可以将父主题更改为你的主题的名称,你将在你的应用程序的其余部分使用。

<style name="DialogTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

方式二:

<style name="DialogTheme" parent="Theme.AppCompat.Dialog">
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>
</style>

步骤2: 然后把它放在AndroidManifest.xml中

<activity
    android:name="com.example.YourApp.DialogActivity"
    android:theme="@style/DialogTheme" />

步骤3: 确保你的主布局宽度fill_parent或match_parent在activity_dialog.xml中

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    tools:context=".DialogActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

在对话框窗口上指定FILL_PARENT,就像其他人建议的那样,对我来说并不管用(在Android 4.0.4上),因为它只是拉伸了黑色对话框背景来填充整个屏幕。

正常工作的方法是使用最小显示值,但在代码中指定它,以便对话框占用屏幕的90%。

So:

Activity activity = ...;
AlertDialog dialog = ...;

// retrieve display dimensions
Rect displayRectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);

// inflate and adjust layout
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_dialog_layout, null);
layout.setMinimumWidth((int)(displayRectangle.width() * 0.9f));
layout.setMinimumHeight((int)(displayRectangle.height() * 0.9f));

dialog.setView(layout);

一般来说,在大多数情况下,只调整宽度就足够了。

你必须在显示之前设置对话框的高度和宽度(dialog.show())

所以,像这样做:

dialog.getWindow().setLayout(width, height);

//then

dialog.show()
***In Kotlin You can Code like This : -*** 

fun customDialog(activity: Activity?, layout: Int): Dialog {
    val dialog = Dialog(activity!!)
    try {
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setCancelable(false)
        dialog.setContentView(layout)
        dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window!!.setLayout(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        dialog.show()
    } catch (e: Exception) {

    }
    return dialog
}

这里有一个简短的答案,对我来说是有效的(在API 8和API 19上测试)。

Dialog mDialog;
View   mDialogView;
...
// Get height
int height = mDialog.getWindow()
.getWindowManager().getDefaultDisplay()
.getHeight();

// Set your desired padding (here 90%)
int padding = height - (int)(height*0.9f);

// Apply it to the Dialog
mDialogView.setPadding(
// padding left
0,
// padding top (90%)
padding, 
// padding right
0, 
// padding bottom (90%)
padding);