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

时间:2013-11-03 11:18:43

标签: android android-layout android-alertdialog

上下文

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


目标

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

软目标:创建一个AlertDialog,其中包含自定义空ViewGroup(或包含ImageView},并设置对话框的宽度和高度到特定的价值。

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


My Tries,Condensed

的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.

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>

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


相关问题

非常感谢!

0 个答案:

没有答案