android - 如何使警报对话框填充 90% 的屏幕尺寸?

标签 android dialog

我可以很好地创建和显示自定义警报对话框,但即便如此,我在对话框 xml 中有 android:layout_width/height="fill_parent" 它也只有内容一样大。

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

最佳答案

根据 Android 平台开发人员 Dianne Hackborn 在 this 中的说法讨论组帖子,对话框将其窗口的顶级布局宽度和高度设置为 WRAP_CONTENT。要使 Dialog 更大,您可以将这些参数设置为 MATCH_PARENT

演示代码:

    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    Dialog d = adb.setView(new View(this)).create();
    // (That new View is just there to have something inside the dialog that can grow big enough to cover the whole screen.)

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(d.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    d.show();
    d.getWindow().setAttributes(lp);

请注意,属性是在对话框显示后设置的。系统对它们何时设置很挑剔。 (我猜布局引擎必须在第一次显示对话框时设置它们,或者其他什么。)

最好通过扩展 Theme.Dialog 来做到这一点,这样您就不必玩什么时候调用 setAttributes 的猜谜游戏了。 (虽然让对话框自动采用适当的浅色或深色主题或 Honeycomb Holo 主题需要更多的工作。这可以根据 http://developer.android.com/guide/topics/ui/themes.html#SelectATheme 来完成)

关于android - 如何使警报对话框填充 90% 的屏幕尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306503/

相关文章:

facebook - 在我的应用程序设置中看不到 "Enhanced Auth Dialog"设置

android - 对话暂停调用 Activity ?

java - 将时间戳转换为相对时间

android - 如何打开 android 内置的下载应用程序?

android - 以编程方式设置 textSize

android - 如何使用Android Youtube播放器播放特定网址?

android - 当 Retrofit2 出现任何类型的错误时向用户显示警报?

dialog - Dojo Dijit Dialog 相对位置。是否可以?

c# - 消息对话框使用 MahApps.Metro 和 MVVM Light Toolkit 通过 DialogCoordinator 触发 ViewModel

windows - 是否可以避免对话框函数中的静态或全局变量