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

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


当前回答

public static WindowManager.LayoutParams setDialogLayoutParams(Activity activity, Dialog dialog)
    {
        try 
        {
            Display display = activity.getWindowManager().getDefaultDisplay();
            Point screenSize = new Point();
            display.getSize(screenSize);
            int width = screenSize.x;

            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
            layoutParams.copyFrom(dialog.getWindow().getAttributes());
            layoutParams.width = (int) (width - (width * 0.07) ); 
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            return layoutParams;
        } 
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

其他回答

在自定义视图xml中设置android:minWidth和android:minHeight。这可以强制警告不只是包装内容大小。 使用这样的视图应该做到:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:minWidth="300dp" 
  android:minHeight="400dp">
  <ImageView
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/icon"/>
</LinearLayout>

到目前为止我能想到的最简单的方法

如果你的对话框是由垂直线性布局组成的,只需添加一个“高度填充”虚拟视图,它将占据屏幕的整个高度。

例如:

<?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:weightSum="1">

    <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/editSearch" />

    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/listView"/>


   <!-- this is a dummy view that will make sure the dialog is highest -->
   <View
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_weight="1"/>

</LinearLayout>

注意LinearLayout属性中的android:weightSum="1"和dummy View属性中的android:layout_weight="1"

让你的对话成为一种活动。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>
***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
}

您可以使用百分比(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%。

的问候。