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

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


当前回答

如果你使用对话片段,你可以用onResume方法。 这是Xamarin Android的代码,但我认为它很容易理解

public override void OnResume() 
{
    base.OnResume();
    var metrics = Resources.DisplayMetrics;

    double width = metrics.WidthPixels * 0.9;
    double height = metrics.HeightPixels * 0.6;

    this.Dialog.Window.SetLayout((int)width, (int)height);
    this.Dialog.Window.SetGravity(Android.Views.GravityFlags.Center);
}

其他回答

我发现了一个非常简单易行的解决办法

    fun showDialog(){
    val dialog = Dialog(this@DialogActivity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.custom_dialog)
    val txtTitle = dialog.findViewById<TextView>(R.id.txtTitle)
    val btn  = dialog.findViewById<Button>(R.id.button)

    btn.setOnClickListener {
        Toast.makeText(this,"test",Toast.LENGTH_SHORT).show()
    }
    txtTitle.setText("ali")
    dialog.show()

    val window = dialog.window
    window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT)
}

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

首先把它放在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中覆盖对话框的主题。它覆盖了对话框窗口布局,让我有机会设置填充和改变背景的不透明度。这可能不是完美的解决方案,但我希望它能帮助你…:)

部分基于阿南德的回答。这对我来说很管用:

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    val fragmentActivity = requireActivity()
    val v = View.inflate(context, R.layout.fragment_about_dialog, null)
    val dialog = Dialog(fragmentActivity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setContentView(v)

    val wm = fragmentActivity.getSystemService(Context.WINDOW_SERVICE) as WindowManager 

    val display = if (VERSION.SDK_INT >= VERSION_CODES.R) {
        fragmentActivity.display
    } else {
        wm.defaultDisplay // deprecated in API 30
    }

    val size = Point()
    display?.getSize(size)

    val width = size.x - 50
    val height = size.y - 50
    val lp = WindowManager.LayoutParams()
    lp.copyFrom(dialog.window?.attributes)
    lp.width = width
    lp.height = height
    dialog.show()
    dialog.window?.attributes = lp
    
    return dialog
}

对于对话框布局使用constraintLayout:

<androidx.constraintlayout.widget.ConstraintLayout 
        android:id="@+id/dialogLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    ...
</androidx.constraintlayout.widget.ConstraintLayout>

结果:

这在改变屏幕方向时工作得很好。

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 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"