使用零内在大小的自定义视图设置AlertDialog的高度

Mic*_*aël 6 android android-layout android-alertdialog

上下文

一个简单的缝合任务,我想设置AlertDialog包含自定义视图的高度.通过关于这个主题的十几个SO问题,我想出了12个!可能的答案.在尝试了所有这些(每秒尝试15年)之后,似乎在屏幕上敲击我的头也有同样的效果:没有.我想这样做的原因是我的自定义视图没有内在的维度,它只是跨越给定的空间.


目标

这里的问题是指软目标,但我很乐意接受这个目标的答案!

软目标: 创建AlertDialog一个自定义空ViewGroup(或包含,例如,一个ImageView),并将对话框的宽度和高度设置为特定值.

硬目标:当以编程方式给出对话框的尺寸时,创建一个尽可能占用空间AlertDialog方形形状.


我的尝试,凝聚

Java的:

View layout = getLayoutInflater ().inflate (R.layout.alerttest, null);

AlertDialog alert = new AlertDialog.Builder (this)
    .setPositiveButton(R.string.alert_dialog_ok, null)
    .setTitle ("Don't cry, will you?")
    // .setView (layout) may be done here.
    .create();

alert.show ();

// alert.setView (layout) here, or before .show, or I can use the doc's:

FrameLayout fl = (FrameLayout) alert.findViewById (android.R.id.custom);
fl.addView (layout, new LayoutParams (500, 500)); // Arbitrary dimensions

alert.getWindow ().setLayout (800, 600);
// Or use before show (), or use alert.getWindow ().setAttributes with
// a WindowManager.LayoutParams crafted from getWindow ().getAttributes.
// Tried before and after show.
Run Code Online (Sandbox Code Playgroud)

alerttest.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Tried with and without the encapsulating FrameLayout. -->
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <LinearLayout android:layout_width="500dp"
                  android:layout_height="500dp">
        <!-- Tried with a Space, an ImageView with a src. -->
        <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

结果:当它没有崩溃时(通常有很好的理由),只设置宽度.高度崩溃了.


相关问题

非常感谢!